Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Weird behavior: os.execute #19410

Closed
marcosantos98 opened this issue Sep 22, 2023 · 7 comments
Closed

Weird behavior: os.execute #19410

marcosantos98 opened this issue Sep 22, 2023 · 7 comments
Labels
Bug This tag is applied to issues which reports bugs.

Comments

@marcosantos98
Copy link
Contributor

marcosantos98 commented Sep 22, 2023

Describe the bug

Brief Explanation about Javac:

When trying to compile java files with inner classes, the javac will generate:

  • Foo.class
  • Foo$Bar.class <- Inner class file
package a.a;

public class Foo {

    public static class Bar {

        public static void baz(){
              System.out.print("+1");
        }
    }
}

THE bug:

This bug happen while trying to compile java source files with inner classes, but for some reason the inner class files wouldn't generate. (Bar class in the example above)

After looking at the V code I found that under the hood os.execute uses:

  • C.popen
  • C.pclose
  • C.fgets

So I created a simple program in C using the same funcs and for some reason, everthing worked fine. So I tried exiting the program after the os.execute to check if the main process would javac end the job and it worked.

res := os.execute(java_cmd)
panic(res)

The above example works fine, but if we remove the panic call, the java compiler doesn't generate all the files, making me think that the process doesn't wait until the child process ends to close it.

Reproduction Steps

  • Create java file with the code above
  • Call os.execute on v source file with `javac [path-to-java-file]
  • Check if Foo$Bar.class was generated

Expected Behavior

os.execute should work the same as 'sh [cmd]`

Current Behavior

os.execute doesn't work as 'sh [cmd]`

Possible Solution

No response

Additional Information/Context

No response

V version

0.4.1

Environment details (OS name and version, etc.)

V full version: V 0.4.1 a0490f2
OS: linux, Linux version 6.5.3-arch1-1 (linux@archlinux) (gcc (GCC) 13.2.1 20230801, GNU ld (GNU Binutils) 2.41.0) #1 SMP PREEMPT_DYNAMIC Wed, 13 Sep 2023 08:37:40 +0000
Processor: 8 cpus, 64bit, little endian, Intel(R) Core(TM) i7-9700K CPU @ 3.60GHz

getwd: /home/marco/dev/open23/jake/example
vexe: /home/marco/dev/soft/v/v
vexe mtime: 2023-09-12 20:09:47

vroot: OK, value: /home/marco/dev/soft/v
VMODULES: OK, value: /home/marco/.vmodules
VTMP: OK, value: /tmp/v_1000

Git version: git version 2.42.0
Git vroot status: 0.4.1-61-ga0490f2b (50 commit(s) behind V master)
.git/config present: true

CC version: cc (GCC) 13.2.1 20230801
thirdparty/tcc status: thirdparty-linux-amd64 12f392c3

Important

You can vote for this issue using the 👍 reaction. More votes increase the issue's priority
for developers.

Take into account that only the 👍 reaction counts as a vote.
Only reactions to the issue itself will be counted as votes, not comments.

@marcosantos98 marcosantos98 added the Bug This tag is applied to issues which reports bugs. label Sep 22, 2023
@JalonSolov
Copy link
Contributor

os.execute() is documented as execute starts the specified command, waits for it to complete, and returns its output.

What is java_cmd?

@marcosantos98
Copy link
Contributor Author

os.execute() is documented as execute starts the specified command, waits for it to complete, and returns its output.

What is java_cmd?

The java_cmd in this case is: javac Subclass.java

  • Subclass/java:
public class Foo {

    public static class Bar {

        public static void baz(){
              System.out.print("+1");
        }
    }
}

@JalonSolov
Copy link
Contributor

Ok... I created Foo.java with your code. I created exetest.v with this:

import os

java_cmd := 'javac Foo.java'
_ := os.execute(java_cmd)

Then I did v run exetest.v.

When that completed, I had this:

$ ls -l Foo*
-rw-r--r-- 1 jalon jalon 443 Sep 22 10:45 'Foo$Bar.class'
-rw-r--r-- 1 jalon jalon 254 Sep 22 10:45  Foo.class
-rw-r--r-- 1 jalon jalon 141 Sep 22 10:40  Foo.java
$

So... looks like it's working, to me.

@marcosantos98
Copy link
Contributor Author

OK. Sorry for the inconvenience. I tried that on my end and it works too, but as soon I implement it on the code itself it doesn't work, but no problem. Thanks non the less.

@JalonSolov
Copy link
Contributor

JalonSolov commented Sep 22, 2023

What do you mean "implement it on the code itself"? Can you show an example that fails?

@JalonSolov JalonSolov reopened this Sep 22, 2023
@marcosantos98
Copy link
Contributor Author

I could but I would look more stupid. This is just one of those cases...

// 1. Cleanup old .class files that don't exist in .java form.
for built_source in os.walk_ext(jake.build_dir_path, 'class') {
     src := built_source.replace('.class', '.java').replace(jake.build_dir_path, jake.src_dir_path)
     if !os.exists(src) {
          os.rm(built_source) or { 
               eprintln("Couldn't remove old .class file ${built_source}. ERR: ${err}")
               exit(1)
          }
     }
}

I was trying to clean up old .class files, so the jar tool didn't include them, by doing so I was removing files that didn't exist in the source file like inner class files.

  • Subclass.class exists under Subclass.java
  • Subclass$Inner.class doesn't exist under Subclass$Inner.java <- This condition made it delete the file

Sorry again for the time spent on a foolish error. I was really convinced that this was a weird bug. Have a good weekend and thanks for the help.

@JalonSolov
Copy link
Contributor

No worries - we've all been there once or twice (ahem!). :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs.
Projects
None yet
Development

No branches or pull requests

2 participants