ENSIME not finding the correct project definition - scala

I'm using an Emacs 24 snapshot on Ubuntu 12.04 with Scala 2.9.2 and ensime_2.9.2-RC1-0.9.3.RC4.
Basically I did the same thing as described in https://groups.google.com/forum/?hl=en&fromgroups#!topic/ensime/HvvvFr5gSwg
I generate .ensime file using sbt ensime generate.
I start ENSIME on Emacs with M-x ensime.
It parses all libraries and so on correctly (auto-complete works properly etc.)
However when I try to start the SBT console within Emacs using C-c C-v s, it displays the following error:
[info] Loading project definition from /home/ximyu/.sbt/plugins
[info] Updating {file:/home/ximyu/.sbt/plugins/}default-86f483...
[error] a module is not authorized to depend on itself: default#default-86f483;0.0
[error] {file:/home/ximyu/.sbt/plugins/}default-86f483/*:update:
java.lang.IllegalArgumentException: a module is not authorized to depend on itself: default#default-86f483;0.0
Obviously ENSIME is looking at the wrong directory for project definition. Instead of looking at my project directory it is actually looking at ~/.sbt. Any solution to this?

I have the same issue. I just started with ensime, so I don't really know why this is the case either. Also, my knowlegde on scala & ensime is very limited, so this is only a work-around to something that might be more obvious.
Anyway, this is the problematic ensime function:
(defun ensime-sbt-project-dir-p (path)
"Is path an sbt project?"
(or (not (null (directory-files path nil "\\.sbt$")))
(file-exists-p (concat path "/project/Build.scala" ))
(file-exists-p (concat path "/project/boot" ))
(file-exists-p (concat path "/project/build.properties" ))))
Basically ensime will use above function to search for a folder above the buffer where you hit C-c C-v s to determine the root project folder. As such, an easy fix is to simply add a (empty) .sbt folder into the folder that contains your .ensime project description.

Related

ASDF not finding package in custom directory

I'm new to Common Lisp, I'm using Emacs/SLIME on Windows 10, and I'm trying to wrap my head around how CL and ASDF/packaging works.
I have a custom package 'my-pack' in a directory 'D:\Dropbox\my-packages'.
I have created a .conf file in:
%LOCALAPPDATA%\config\common-lisp\source-registry.conf.d\
And added this line:
(:tree "D:\\Dropbox\\my-packages\\")
I opened Emacs, started SLIME and made the project via the REPL:
(cl-project:make-project #p"D:/Dropbox/my-packages/my-pack)
I verified that the project is in the directory and then loaded the system with asdf (version 3.3.1):
(asdf:load-system :my-pack)
And it worked fine.
But when I quit and restart Emacs, I get the following error when trying to the load the system:
Component :MY-PACK not found
[Condition of type ASDF/FIND-COMPONENT:MISSING-COMPONENT]
As far as I can tell I've followed the steps in the manual. Any help appreciated.
cl-project's make-project ends with this line:
(push dir asdf:*central-registry*)
it adds your new project's directory to this list that tells ASDF where to look for projects. What is its value when you restart CL?
2.
\config\common-lisp\
Shouldn't it be .config?
However, I don't encourage to use this conf file with :tree. The doc says:
tell ASDF to recursively scan all the subdirectories
So, imagine that just once, you try yourself at web development and you install, for example, JavaScript dependencies with npm or equivalent, you'll end up with a gigantic node_modules/ and your Lisp will now take a few seconds to start up.
I suggest to put your projects under ~/common-lisp/ or ~/quicklisp/local-projects, or to create symlinks, or to add yourself your projects in asdf:*central-registry* from your Lisp startup file:
;; .sbclrc
(pushnew "/home/me/projects/ciel/" asdf:*central-registry* :test #'equal)

On Using clojure.tools.namespace

In emacs cider repl, now I know how to use clojure.tools.namespace in a leiningen project. However, when I use it on a single clj file which doesn't belong to any project, it seems clojure.tools.namespace doesn't work on the file:
=> #<FileNotFoundException java.io.FileNotFoundException: Could not locate com/foo__init.class or com/foo.clj on classpath: >
I have declared clojure.tools.namespace in .lein/profile.clj and require it in the clj file. How should I make clojure.tools.namespace work on a single clj file?
My profile.clj
{:user
{:repl-options {:timeout 128000}
:plugins [;; REPL
[cider/cider-nrepl "0.9.0-SNAPSHOT"]
[refactor-nrepl "0.2.2"]
;; Application server
[lein-immutant "2.0.0-SNAPSHOT"]
;; Automated testing
[lein-cloverage "1.0.2"]
[lein-test-out "0.3.1"]
;; Package management
[lein-ancient "0.6.2"]
[lein-clojars "0.9.1"]
;; Documentation
[codox "0.6.8"]
[lein-clojuredocs "1.0.2"]
;; Static analysis
[lein-typed "0.3.4"]
;; [jonase/eastwood "0.1.2"]
[lein-bikeshed "0.1.6"]
[lein-kibit "0.0.8"]]
:jvm-opts ["-Dapple.awt.UIElement=true"]
:dependencies [[org.clojars.gjahad/debug-repl "0.3.3"]
[difform "1.1.2"]
[spyscope "0.1.4"]
[org.clojure/tools.trace "0.7.8"]
[org.clojure/tools.namespace "0.2.9"]
[im.chit/vinyasa "0.2.0"]
[slamhound "1.5.5"]
[criterium "0.4.3"]]
:injections [(require 'spyscope.core)
(require 'alex-and-georges.debug-repl)
(require 'com.georgejahad.difform)
(require '[vinyasa.inject :as inj])
(inj/inject 'clojure.core '>
'[[clojure.repl apropos dir doc find-doc pst source]
[clojure.tools.trace trace trace-forms trace-ns trace-vars
untrace-ns untrace-vars]
[clojure.test run-tests run-all-tests]
[clojure.pprint pprint pp]
[com.georgejahad.difform difform]
[alex-and-georges.debug-repl debug-repl]
[vinyasa.pull pull]])]}}
While this doesn't answer your specific question, it might provide some ideas on
alternative workflows which may help.
lein-exec This is a lein plugin that
lets you write clojure scripts or evaluate clojure expressions on the command line
in a similar way to what can be done with shells, python, ruby, perl etc. Of
course, startup time is a bit high, but this plugin will help you deal with
dependencies etc inside your single clj script file. (there are other possible
solutions to improve the startup speed). See this blog post for some examples
http://charsequence.blogspot.in/2012/04/scripting-clojure-with-leiningen-2.html
A scratch project. I have a fairly generic project called scratch. It just allows
me to create a new file within the src directory of the project which I can use for
experiments or demonstrations. For example, I have a file called stackoverflow.clj
within this scratch project which I use when working out the answer to a clojure
question on stack overflow. In fact, I have a lot of individual clj files in this
directory, each just representing a simple idea, test, experiment etc. It isn't
really a project i.e. I couldn't do a lein run at the root of the project and
expect anything meaningful. However, I can go to the source directory in emacs,
open a file and then run cider.

Using local jar files with emacs clojure/cider mode and REPL

I'm trying to use functions from other jar files.
Creation of local jar file
I downloaded sample sources from the book Programming Clojure 2nd Ed, and created a jar file with lein jar command.
Use the local jar file
From the hints in this post, I copied the jar file in lib/ directory, then I could add
:resource-paths ["lib/programming-clojure-1.3.0.jar"] in the project.clj.
Test in REPL
With lein classpath command, I could check that the jar file is in class path.
With lein repl, I could use the functions in the jar file.
mire=> (require '[examples.introduction :as e])
nil
mire=> (take 10 examples.introduction/fibs)
(0 1 1 2 3 5 8 13 21 34)
Isses with emacs/cider
I created a t.clj source in the src/ directory, launched emacs with emacs src/t.clj &, and started REPL with M-x cider-jack-in.
I wrote this code, and executed it with C-x C-e.
(ns t
(:require '[examples/introduction :as ex]))
However, I got a message that the file is not found.
java.io.FileNotFoundException: Could not locate introduction__init.class or introduction.clj on
classpath:
What might be wrong?
The namespace is examples.introduction. Also, you don't need to quote the vector inside the ns macro. Try:
(ns t
(:require [examples.introduction :as ex]))
See some examples of use for the ns macro here.
Also, it's customary to have at least two segments in namespaces. Yours could be mynamespace.t for example.

Ensime inferior scala mode

I've setup Emacs + Ensime for scala.
I'm able to start sbt console inside emacs using C-c C-v s
If i start scala console inside emacs using C-c C-v z, I get the following error
Welcome to Scala version 2.9.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_33).
scala>
Failed to initialize compiler: class scala.reflect.BeanInfo not found
What is the fix for this error? How do i get scala console running inside Emacs?
I've ran into a similar error recently, but here's how I worked around it (but I don't know how to fix it, so this is only half the answer). What I did was to customize the ensime-inf-default-cmd-line variable to have the value: '("sbt" "console"). Which will indeed start the interactive Scala environment.
EDIT:
Here's relevant parts from .emacs, but I'm not sure it will matter / will be the same in every install:
(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
;;
;; more stuff ...
;; I'm not sure this line is correct / makes any difference
'(ensime-inf-cmd-template (quote ("sbt" "console" "-classpath" :classpath)))
;; This variable is used to launch the interpreter
'(ensime-inf-default-cmd-line (quote ("sbt" "console"))))
(require 'scala-mode2)
(add-to-list 'auto-mode-alist '("\\.scala$" . scala-mode))
(add-to-list 'load-path "~/.emacs.d/ensime/elisp/")
(require 'ensime)
(add-hook 'scala-mode-hook 'ensime-scala-mode-hook)
I've installed scala-mode2 from MELPA. Installed sbt version 0.12.0 by downloading an RPM from their site. I had previously JRE and JDK installed, the active version is OpenJDK 1.7. Scala installed is 2.9.2. I don't know how to identify Ensime's version :|

Invoking makefile in the project root directory from subdirectory from Emacs

I have a makefile in the project root directory. If I am editing a file in a subdirectory, how do I invoke make from EMACS? M-x compile make will not work as it looks for the makefile in the current directory. But I have the makefile in the project root directory.
Any thoughts?
Edit
As suggested, make -f fullpath_to_makefile did the trick. But I have some includes in the makefile like include "tests/module.mk" which failed. It is looking for "tests" directory in the subdirectory. This can be solved by specifying fully qualified path in the makefile. But I don't think that is a good solution. Any better approaches?
The M-x compile function looks up the compile-command-variable which you can override on the promt -- so just replace it with something like
(cd ../.. && make && cd -)
and let that run.
I also often use a file header (in line 1) such as
// -*- compile-command: "g++ -o myprog myprog.ccc -lfoo -lbar && ./myprog"; -*-
which you can generalize at will with different options. After reloading the file, M-x compile will execute your custom compile command which I find quite useful.
(I use scons, but the principle is the same. Change SConstruct to Makefile and scons to make...)
I've customized by .emacs so that it always compiles the project containing the current buffer's file, however deeply nested; it searches upwards for the first SConstruct and uses that as it's project root directory.
Here's a couple of functions which search up the directory hierarchy looking for SConstruct.
;; inspired by jds-find-tags-file in http://www.emacswiki.org/emacs/EmacsTags
(defun find-sconstruct ()
"recursively searches upwards from buffer's current dir for file named SConstruct and returns that dir. Or nil if not found or if buffer is not visiting a file"
(labels
((find-sconstruct-r (path)
(let* ((parent (file-name-directory path))
(possible-file (concat parent "SConstruct")))
(cond
((file-exists-p possible-file)
(throw 'found-it possible-file))
((string= "/SConstruct" possible-file)
(error "No SConstruct found"))
(t (find-sconstruct-r (directory-file-name parent)))))))
(if (buffer-file-name)
(catch 'found-it
(find-sconstruct-r (buffer-file-name)))
(error "Buffer is not visiting a file"))))
(defun project-root ()
(file-name-directory (find-sconstruct)))
You can then change your compile-command to use project-root e.g.
(concat "cd " (project-root) " && scons")
I use EDE (from CEDET) to define projects, and store compilation commands in the project definition. Look to my config for examples: lines 105-133 -- examples of projects, lines 135-165 -- code, that defines compilation functions, and lines 168-189 -- functions for different kinds of projects -- standard (compile from root directory), and cmake (compilation in separate directory)
Another alternative is to set the variable compilation-process-setup-function which is documented as:
Function to call to customize the compilation process. This function
is called immediately before the compilation process is started. It
can be used to set any variables or functions that are used while
processing the output of the compilation process. The function is
called with variables compilation-buffer' andcompilation-window'
bound to the compilation buffer and window, respectively.
I use Maven alot and wrote this library to support your issue for a Maven context. In the following, change the value of the variable compile-search-file as appropriate:
;;; Support for Maven 2
(require 'compile)
(setq compile-search-file "pom.xml")
(defun find-search-file ()
;; Search for the pom file traversing up the directory tree.
(setq dir (expand-file-name default-directory))
(let ((parent (file-name-directory (directory-file-name dir))))
(while (and (not (file-readable-p (concat dir compile-search-file)))
(not (string= parent dir)))
(setq dir parent
parent (file-name-directory (directory-file-name dir))))
(if (string= dir parent)
(error "Search file %s is missing" compile-search-file)
(with-current-buffer compilation-last-buffer
(message "Test %s %s." compilation-buffer compilation-window)
(setq default-directory dir)))))
;; Add the following to support Emacs' compile mode:
(add-to-list
'compilation-error-regexp-alist-alist
'(mvn "^\\(.*\\):\\[\\([0-9]*\\),\\([0-9]*\\)\\]" 1 2 3))
(add-to-list 'compilation-error-regexp-alist 'mvn)
(setq compilation-process-setup-function 'find-search-file)
(provide 'maven-support)
When I use Emacs to compile the minibuffer looks something like this;
make -k
and I just add any text I like, such as -f ../../root/Makefile. This might work for you.
Mark
I'm not much of an emacs user, but could you pass make -C ../ or however many directories up you need to go?
After a while of different attempts to make EDE work the way I wanted, I went for .dir-locals.el:
((c++-mode . ((compile-command . "make -C ../build -j2 whatever"))))
I found it slightly better for me than having a // -*- -*- in a header of every file,
and a whole lot better than specifying in my init.el (or any other config) those ede-cpp-root-project with full paths to projects, which I either create too often or move all of a sudden :)
Nice addition to the scheme was cmake which makes compile errors to be 'properly jumpable' since it uses full paths in generated makefiles.
I've just started working on a more generic, extensible, and robust yet still reasonably quick-and-dirty solution I just created. It's somewhat based on the Maven example above but I prefer not mucking around with global variables so I use the let or let* special forms a lot more. And of course it uses make.
Right now it only supports Makefiles but you can add a clause to the (cond) special form in the `my-compilation-process-setup-function' function if you want to support one or more additional different build systems.
It even has doc strings!
You'll see it at my Github eventually.
This should do it:
make -f path_to_rootdir/Makefile -I path_to_rootdir/tests
The -f tells it what makefile to use, the -I tells it where to look for files to be included in the makefile (that aren't in the local directory).
Start with M-x compile RET. When it prompts for a command, just enter cd /path/to/root && make and hit return. This command works for all variants of make and handles the "included makefile" problem without any extra flags.
The next time you type M-x compile RET, this new string will be presented as the default, so you only have to hit return. On the off chance that you're actively compiling multiple projects within emacs, you can use M-p and M-n to move backwards and forwards through the history of compile comamnds.