kaby76 / Antlr4BuildTasks

Third-party build tool for 'Official' Antlr4 tool and runtime parsers using .Net. Drop-in replacement for 'Antlr4cs' Antlr4 tool and build rules.
MIT License
75 stars 10 forks source link

'JRE file used by another process' error when building through Azure DevOps #77

Closed Paar86 closed 8 months ago

Paar86 commented 8 months ago

Hi. I'm using the package in my company's project and it was a success for a long time. However after updating to .NET 8 and the latest version of Antlr4BuildTasks I'm getting errors when building the project in Azure DevOps:

Downloading OpenJDK11U-jre_x64_windows_hotspot_11.0.15_10.zip
  Completed downloading of OpenJDK11U-jre_x64_windows_hotspot_11.0.15_10.zip
  Found C:/Users/VssAdministrator/.jre/OpenJDK11U-jre_x64_windows_hotspot_11.0.15_10.zip
  Downloaded JRE. Testing for java executable at C:/Users/VssAdministrator/.jre/jdk-11.0.15+10-jre/bin/java.exe
  C:/Users/VssAdministrator/.jre/jdk-11.0.15+10-jre/bin/java.exe does not seem to exist. Decompressing.
  Decompressing
  Create directory apparently worked.
  Caught throw in extraction code.
##[error]C:\Users\VssAdministrator\.nuget\packages\antlr4buildtasks\12.7.0\build\Antlr4BuildTasks.targets(138,9): Error ANT02: The process cannot access the file 'C:\Users\VssAdministrator\.jre\OpenJDK11U-jre_x64_windows_hotspot_11.0.15_10.zip' because it is being used by another process.   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
   at System.IO.Compression.ZipFile.Open(String archiveFileName, ZipArchiveMode mode, Encoding entryNameEncoding)
   at System.IO.Compression.ZipFile.ExtractToDirectory(String sourceArchiveFileName, String destinationDirectoryName, Encoding entryNameEncoding)
   at Antlr4.Build.Tasks.RunAntlrTool.DecompressJava(String uncompressed_root_dir, String archive_name)

The build task successfully downloads the JRE installer but is unable to extract the .zip file because somethings keeps it locked. Has there been any change in the package that could result in such behavior?

This is what we've been using previously with .NET 7:

<ItemGroup>
    <PackageReference Include="Antlr4.Runtime.Standard" Version="4.9.3" />
   <PackageReference Include="Antlr4BuildTasks" Version="8.17.0" />
</ItemGroup>

Currently we're using .NET 8 with:

<ItemGroup>
    <PackageReference Include="Antlr4.Runtime.Standard" Version="4.13.1" />
    <PackageReference Include="Antlr4BuildTasks" Version="12.7.0" />
</ItemGroup>

Thanks for any input!

kaby76 commented 8 months ago

I will look into it.

Paar86 commented 8 months ago

Thanks, hopefully you'll find something.

kaby76 commented 8 months ago

It seems we've had this problem for a while. In a build, the Antlr4BuildTasks package downloaded the Java JRE .zip for running the Antlr tool, but then tries to extract the file in a multi-process environment trying to compete for the .zip. The easiest is to unzip the file to some directory, find the java.exe, and make sure the PATH environment contains the directory containing that executable.

Paar86 commented 8 months ago

Good to know it's not a problem on my end. Will you be fixing the issue in the near future? I'll try the solution you suggest in meantime.

Pansynchro-Admin commented 8 months ago

Seeing the same issue on GitHub Actions. Since updating to the latest version, our project will not build.

kaby76 commented 8 months ago

Seeing the same issue on GitHub Actions. Since updating to the latest version, our project will not build.

Reproducing it now. Looking at it.

Have you tried the work around? (Install a JRE -- see list here https://github.com/kaby76/Antlr4BuildTasks/blob/72071e1842cc6f26886ea6ecf80c3c495bf64fe7/Antlr4BuildTasks/Tasks/RunAntlrTool.cs#L43 or install an OpenJDK and add the directory containing java to your path, then try again.)

Pansynchro-Admin commented 8 months ago

Have you tried the work around?

No; how exactly do you do that on a GitHub server where you don't have shell access to install things?

kaby76 commented 8 months ago

No; how exactly do you do that on a GitHub server where you don't have shell access to install things?

It's not hard. Just copy these lines to your .github/workflows/main.yml file to install and test the java installation. https://github.com/antlr/grammars-v4/blob/caf66c3d8a4657a4871a539fa8de9c9a6b5a8b1b/.github/workflows/main.yml#L83-L92 . Github Actions has a number of different shells to install what you want if you need to do things via a shell script.

Pansynchro-Admin commented 8 months ago

OK, that does seem to be an effective workaround.

Meanwhile, I've been trying different versions. The problem doesn't seem to exist in v12.5 and earlier, so whatever caused it is a pretty recent change.

kaby76 commented 8 months ago

Meanwhile, I've been trying different versions. The problem doesn't seem to exist in v12.5 and earlier, so whatever caused it is a pretty recent change.

Something changed with .NET version 8 that I haven't accounted for yet.

kaby76 commented 8 months ago

Found a problem. USERPROFILE not being expanded with env info.

kaby76 commented 8 months ago

File is being downloaded but it's zero bytes at the time of decompressing. Logic to wait for the completion of the download broken. I changed the code from WebClient() to DownloadFileAsync() because WebClient() is obsolete and was replaced. The Wait() isn't working, probably because DownloadFileAsync doesn't return a completion object that makes any sense. https://github.com/kaby76/Antlr4BuildTasks/blob/72071e1842cc6f26886ea6ecf80c3c495bf64fe7/Antlr4BuildTasks/Tasks/RunAntlrTool.cs#L808C85-L808C85 . The TimeSpan seems to be correct (hours, minutes, seconds) https://learn.microsoft.com/en-us/dotnet/api/system.timespan.-ctor?view=net-8.0#system-timespan-ctor(system-int32-system-int32-system-int32)

kaby76 commented 8 months ago

Fixed version 12.8. Let me know if it fails.