jbang icon indicating copy to clipboard operation
jbang copied to clipboard

jbang edit does not commit changes

Open afloarea opened this issue 2 years ago • 11 comments

Describe the bug Using the jbang edit command does not commit changes to the script file.

To Reproduce Steps to reproduce the behavior:

  1. jbang init hello
  2. jbang edit --open=codium hello
  3. Change 'Hello World' string to 'Hello Jbang', save file and close editor.
  4. Make the file executable and run it.

Expected behavior I expect to see 'Hello Jbang' but the output is still 'Hello world'. The file is not modified.

JBang version

[jbang] jbang version 0.102.0
Cache: /home/afloarea/.jbang/cache
Config: /home/afloarea/.jbang
Repository:/home/afloarea/.m2/repository
0.102.0

Additional context I might have the wrong expectation here or I may be mistaken, I searched the docs for an option to write through but I couldn't find one. I can see that running the edit command creates a folder in ~/.jbang/cache/projects with a source hello.java that is a symlink to .jbang/cache/scripts/<some-hash>/hello.java. This file is indeed updated, but the original file that I wanted to edit is unchanged.

Thank you for your time and for maintaining this awesome project!

afloarea avatar Feb 20 '23 17:02 afloarea

What OS are you on?

Can you show the exact steps and output when you run?

maxandersen avatar Feb 20 '23 17:02 maxandersen

I am using Linux/Manjaro

$ cd /tmp
$ jbang init hello
[jbang] File initialized. You can now run it with 'jbang hello' or edit it using 'jbang edit --open=[editor] hello' where [editor] is your editor or IDE, e.g. 'code'
$ chmod +x hello
$ ./hello
Hello World
$ cat hello
///usr/bin/env jbang "$0" "$@" ; exit $?


import static java.lang.System.*;

public class hello {

    public static void main(String... args) {
        out.println("Hello World");
    }
}

$ jbang edit --open=codium hello
[jbang] Starting 'codium'.If you want to make this the default, run 'jbang config set edit.open codium'
/home/afloarea/.jbang/cache/projects/hello.java_jbang_b0492628f14e54dc90b772b7908a391535ed3d357639d5fb4d52efe3b3e2e09a/hello

At this point the editor opens up and I change the hello string in the file, save and quit de editor. But if I view the file, it is unchanged

$ cat hello
///usr/bin/env jbang "$0" "$@" ; exit $?


import static java.lang.System.*;

public class hello {

    public static void main(String... args) {
        out.println("Hello World");
    }
}

$ cat /home/afloarea/.jbang/cache/projects/hello.java_jbang_b0492628f14e54dc90b772b7908a391535ed3d357639d5fb4d52efe3b3e2e09a/hello/src/hello.java
///usr/bin/env jbang "$0" "$@" ; exit $?


import static java.lang.System.*;

public class hello {

    public static void main(String... args) {
        out.println("Hello Jbang");
    }
}

$ ls -l /home/afloarea/.jbang/cache/projects/hello.java_jbang_b0492628f14e54dc90b772b7908a391535ed3d357639d5fb4d52efe3b3e2e09a/hello/src/hello.java
lrwxrwxrwx 111 afloarea 20 feb 18:52  /home/afloarea/.jbang/cache/projects/hello.java_jbang_b0492628f14e54dc90b772b7908a391535ed3d357639d5fb4d52efe3b3e2e09a/hello/src/hello.java -> /home/afloarea/.jbang/cache/scripts/6fd5eee74ec3af4e2bec2ec00c97dd1409a8d6f7514e1e70c847c3d5f80fc8f2/hello.java

I believe that I encountered a similar issue quite some time ago on Windows using git bash, but I thought at the time that maybe symlinks were not working properly, but this was sometime last year.

If there's anything else I can help with, please let me know. Thank you for the quick reply.

afloarea avatar Feb 20 '23 22:02 afloarea

@afloarea that's indeed an issue that we need to look into.

For now you can work around this issue by using a "proper" java file name, eg "hello.java".

quintesse avatar Feb 21 '23 01:02 quintesse

@maxandersen this is because for files with a "wrong" name we make a copy with a "correct" name and build/run the copy instead of the original. Then edit makes a link to that copy, not the original. We have to figure out what's best here. Perhaps for files on the local filesystem we can make a link with the proper name instead? It sounds doable but I haven't thought it through.

quintesse avatar Feb 21 '23 01:02 quintesse

I believe we did exactly that in the past.

maxandersen avatar Feb 21 '23 07:02 maxandersen

I believe we did exactly that in the past.

I think the first usage of links was with edit, and for the longest time it wasn't used for anything else. Then we started using it for the default JDK as well. In both cases we've had problems at times with Windows. We've never had problems with links and basic functionality like building and running.

And according to our commit history this is 2 1/2 years ago:

https://github.com/jbangdev/jbang/blob/406b20861edb521426afca208d9c7dc757ef6373/src/main/java/dev/jbang/cli/BaseScriptCommand.java#L151-L165

And it was copying, so I'm assuming it was always like that.

Edit: oh wow, is that new, that it shows the code in-line if I paste a link to a range of code? Cool!

quintesse avatar Feb 21 '23 11:02 quintesse

So IMHO if we want to fix this with sym links we need to make really sure that we got this rights issue on Windows solved first.

quintesse avatar Feb 21 '23 12:02 quintesse

Ooooh. Yeah. This is borked because to have it working in ide the # line has to be transformed.

We should probably stop doing it or print warning as No way to both have runnable and editable.

maxandersen avatar Feb 21 '23 13:02 maxandersen

Yeah, it seems that since we tell people to use /// instead of #! we could just remove that transformation. That way we can use links and make things work as expected.

Edit: we could check if that line exists, tell people to use /// instead and exit.

quintesse avatar Feb 21 '23 16:02 quintesse

Issue is that #! Is required for ie. Git / kubectl plugins.

maxandersen avatar Feb 21 '23 16:02 maxandersen

Darn. Well then the only option I see is to give that warning when both coincide: someone wanting to edit a script that starts with #! and explain the possible solutions.

quintesse avatar Feb 21 '23 17:02 quintesse