Bug 66293 - Ant doesn't detect windows junctions as symlinks
Summary: Ant doesn't detect windows junctions as symlinks
Status: NEW
Alias: None
Product: Ant
Classification: Unclassified
Component: Core (show other bugs)
Version: 1.10.5
Hardware: PC All
: P2 normal (vote)
Target Milestone: ---
Assignee: Ant Notifications List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2022-10-03 18:06 UTC by Maarten Coene
Modified: 2022-10-10 21:01 UTC (History)
0 users



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Maarten Coene 2022-10-03 18:06:21 UTC
When a directory is a Windows NTFS Junction, Ant does not recognize it as a symbolic link.

I think this used to work in Ant 1.9.x (not tested though), but it is broken since the refactoring to use the Files.isSymbolicLink method. This method doesn't detect NTFS Junctions.

Possible solutions to detect junctions are described in this post: https://stackoverflow.com/questions/13733275/determine-whether-a-file-is-a-junction-in-windows-or-not

For instance:
boolean isWindows = System.getProperty("os.name").toLowerCase().contains("windows")
BasicFileAttributes attrs = Files.readAttributes(aPath, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
boolean isJunction = isWindows && attrs.isDirectory() && attrs.isOther();

Maarten
Comment 1 Jaikiran Pai 2022-10-04 01:47:41 UTC
Hello Maarten, I don't have access to a Windows machine, so can't easily check. Do you have some Ant build/example where this behaviour impacts Ant? That might give us a better idea about what kind of impact this has.

P.S: I hadn't heard of Windows junctions before, so need to check what kind of support Java itself provides for those.
Comment 2 Maarten Coene 2022-10-10 12:01:26 UTC
Hi Jaikiran,

consider following build.xml:

<project name="bug 66293">
  <target name="clean">
    <delete includeemptydirs="true">
      <fileset dir="build" followsymlinks="false" />
    </delete>
  </target>
</project>

Say I have the following directory structure:

/build.xml
/src/test.txt
/build/src -> junction to /src

If I run the Ant script, both the '/build' directory and the '/src/test.txt' file are deleted.

I tested it with Ant 1.9 as well, it has the same issue, so it's not a regression due to the symlink refactoring.
Comment 3 Stefan Bodewig 2022-10-10 16:44:51 UTC
I don't believe Ant has ever properly supported junctions - and the manual or FAQ may even says so in some place.

Adding support would be a nice enhancement, of course.

I must admit I have no idea how widely used junctions are.
Comment 4 Maarten Coene 2022-10-10 21:01:03 UTC
I don't know how often they are used either. I'm a Windows user for a very long time and never saw them in action. But I ran into this issue when using npm on Windows. Seems like I'm not the only one, this is exactly the issue I had: https://github.com/npm/cli/issues/5189

Yes I also think it would be great if we could support that. The stackoverflow link I posted earlier contains 2 possible suggestions for how to detect Windows Junctions using Java8 API.

- Using the Path.toRealPath() method: https://stackoverflow.com/a/29699473
- Using BasicFileAttributes: https://stackoverflow.com/a/48291462

Do you have a preference? I can try to submit a patch for this.