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

Create a tmp directory when such doesn't exist. In some cases the tmp… #10002

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

drullar
Copy link

@drullar drullar commented Nov 21, 2024

Description

While writing Jenkins plugin tests and debugging them in IntelliJ I stumbled upon an issue when trying to create an instance of JenkinsRule, which was failing to instantiate due to the following exception:

java.lang.Error: java.nio.file.NoSuchFileException: .../plugin-dir/target/tmp/jenkins17059940806884222895
at org.jvnet.hudson.test.TestPluginManager.<clinit>(TestPluginManager.java:45)
at org.jvnet.hudson.test.JenkinsRule.<init>(JenkinsRule.java:345)
at com.appfire.jenkins.plugins.it.TestDeflakeBuild.<init>(TestDeflakeBuild.java:10)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
at java.base/java.util.Optional.orElseGet(Optional.java:364)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
Caused by: java.nio.file.NoSuchFileException: .../plugin-dir/target/tmp/jenkins17059940806884222895
at java.base/sun.nio.fs.UnixException.translateToIOException(UnixException.java:92)
at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:106)
at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:111)
at java.base/sun.nio.fs.UnixFileSystemProvider.createDirectory(UnixFileSystemProvider.java:397)
at java.base/java.nio.file.Files.createDirectory(Files.java:700)
at java.base/java.nio.file.TempFileHelper.create(TempFileHelper.java:134)
at java.base/java.nio.file.TempFileHelper.createTempDirectory(TempFileHelper.java:171)
at java.base/java.nio.file.Files.createTempDirectory(Files.java:1017)
at hudson.Util.createTempDir(Util.java:437)
at org.jvnet.hudson.test.TestPluginManager.<init>(TestPluginManager.java:50)
at org.jvnet.hudson.test.TestPluginManager.<clinit>(TestPluginManager.java:28)
... 7 more

I was debugging the test from within IntelliJ and what I noticed was that IntelliJ was setting the java.io.tmpdir property to be a tmp directory within the project target directory, which doesn't exist my default, hence why I am creating this PR. This issue could have been resolved with extra maven hackery in my plugin's pom.xml, but I thought that this issue might occur in different scenarios as well and why not handle it here.

… director won't exist and an Exception will be thrown when attempting to create a file in that directory. An example of such case is creating a JenkinsRule instance in some tests and executing those tests from an IDE.
Copy link

welcome bot commented Nov 21, 2024

Yay, your first pull request towards Jenkins core was created successfully! Thank you so much!

A contributor will provide feedback soon. Meanwhile, you can join the chats and community forums to connect with other Jenkins users, developers, and maintainers.

if (!systemTmpDirectoryPath.toFile().exists()){
// In some cases the tmp directory set in the java.io.tmpdir property will not exist and hence will have to
// be created here.
systemTmpDirectoryPath.toFile().mkdirs();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using Files.createDirectory instead should avoid the SpotBugs warning.

@@ -436,6 +436,14 @@ public static File createTempDir() throws IOException {
// https://github.com/jenkinsci/jenkins/pull/3161 )
final Path tempPath;
final String tempDirNamePrefix = "jenkins";

final Path systemTmpDirectoryPath = Path.of(System.getProperty("java.io.tmpdir"));
if (!systemTmpDirectoryPath.toFile().exists()){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!systemTmpDirectoryPath.toFile().exists()){
if (!systemTmpDirectoryPath.toFile().exists()) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants