Skip to content
This repository has been archived by the owner on Sep 8, 2022. It is now read-only.

manually check for a .exe #71

Merged
merged 1 commit into from
Jan 17, 2017
Merged

Conversation

martijnhoekstra
Copy link
Contributor

canExecute does not guarantee an executable file, and will find java.dll before java.exe on Windows

@martijnhoekstra
Copy link
Contributor Author

fixes #70

(candidates find (_.name == name) orElse candidates.headOption) map (_.path)
val plain = candidates find (_.name == name)
val exe = candidates find (_.name == s"${name}.exe")
(plain orElse exe orElse candidates.headOption).map(_.path)
Copy link
Contributor

@som-snytt som-snytt Jan 4, 2017

Choose a reason for hiding this comment

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

Is it plainer to do val realname = if (util.Properties.isWin && !name.endsWith(".exe")) s"$name.exe" else name and find that? Add case-insensitive test, of course. I was a little clever with the dot test to avoid these platform dances.

Actually the filter should read e.name == name || isWin && e.name.looksLike(name.exe). Then assert there's at most one candidate, with a nice message, to avoid future confusion.

Copy link
Contributor Author

@martijnhoekstra martijnhoekstra Jan 5, 2017

Choose a reason for hiding this comment

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

In practice, if there is neither a name or name.exe, the guess is most likely just wrong. The theoretical platform/jdk/jre that uses the same name but a different extension probably doesn't exist, and abstracting over that hypothetical probably isn't worth the brain cycles.

replacing the whole thing with something like

(p walkFilter { e => (e.name == name || e.name == e.name.looksLike(s"${name}.exe") && e.jfile.canExecute }).map(_.path).headOption

or even just

((Path(jdkHome) / "bin") .walkFilter { e => (e.name == name || e.name == s"${name}.exe") && e.jfile.canExecute }).map(_.path).headOption
should work fine enough. It's not going to find java.woozle if that is the correct file (which it isn't). If it's looking for a file named bin (which it isn't) and jdkhome/bin is a file rather than a directory (which it also isn't) it's going to return the path of jdkhome/bin rather than None mistakenly. I'm not sure that would be a great loss.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, even simpler.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll update the PR to whichever is your preference when I'll get home tonight

Copy link
Contributor

Choose a reason for hiding this comment

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

Your second expr gets it done. With paranoid case insensitive.

(Path(jdkHome) / "bin").walk.find(e => e.name == name || isWin && e.name.equalsIgnoreCase(s"$name.exe")).map(_.path)

When I looked yesterday, the doc on walkFilter (there's doc) inverts the sense of filter, so I wouldn't assume anything about experimental i/o library. But walk on a regular file seems OK.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wouldn't assume anything about the doc either:

  /** Equivalent to walkFilter(_ => false).
   */
  def walk: Iterator[Path] = walkFilter(_ => true)

Copy link
Contributor

Choose a reason for hiding this comment

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

OK I missed that one. That's so funny.

@som-snytt
Copy link
Contributor

LGTM

I left a comment but it's 6/13 of a baker's dozen.

@retronym
Copy link
Member

retronym commented Jan 5, 2017

FTR, CreateProcess appends .exe if no extension is provided, which is why just spawning $JAVA_HOME/bin/java works on Windows.

@som-snytt
Copy link
Contributor

If only the Scala team had a Windows expert on staff.

canExecute does not guarantee an executable file, and will find java.dll
before java.exe on Windows
@SethTisue SethTisue merged commit 0ec12a2 into scala:master Jan 17, 2017
@SethTisue
Copy link
Member

thanks Martijn!

lrytz pushed a commit to lrytz/scala-partest that referenced this pull request May 9, 2018
lrytz pushed a commit to lrytz/scala-partest that referenced this pull request May 9, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants