Skip to content

Commit

Permalink
[#242] No scan symlinks as md files
Browse files Browse the repository at this point in the history
Problem: when the repository contains a symlink to a markdown file, it
is processed by xrefcheck as if it was the same markdown file but in the
symlink's location. This leads to broken references and can be avoided
because neither GitHub nor GitLab try to render symlinks as the file
they point to.

Solution: consider symlinks as no scannable files. In the future, we
will consider to include a new dedicated scanner for symlinks if it
works.
  • Loading branch information
aeqz committed Jan 12, 2023
1 parent 754e437 commit 066da26
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Xrefcheck/Scan.hs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,10 @@ scanRepo scanMode rw formatsSupport config root = do
gatherFileStatuses = map (second fst)

processFile :: CanonicalPath -> IO (FileStatus, [ScanError 'Parse])
processFile canonicalFile = case mscanner canonicalFile of
processFile canonicalFile =
ifM (pathIsSymbolicLink canonicalFile)
(pure (NotScannable, []))
case mscanner canonicalFile of
Nothing -> pure (NotScannable, [])
Just scanner -> scanner canonicalFile <&> _1 %~ Scanned

Expand Down
5 changes: 5 additions & 0 deletions src/Xrefcheck/System.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module Xrefcheck.System
, getPosixRelativeChild
, getPosixRelativeOrAbsoluteChild
, hasIndirectionThroughParent
, pathIsSymbolicLink
, takeDirectory
, takeExtension
, (</)
Expand Down Expand Up @@ -94,6 +95,10 @@ takeDirectory (UnsafeCanonicalPath p) = UnsafeCanonicalPath $ FP.takeDirectory p
takeExtension :: CanonicalPath -> String
takeExtension (UnsafeCanonicalPath p) = FP.takeExtension p

-- | 'System.Directory.pathIsSymbolicLink' version for 'CanonicalPath'.
pathIsSymbolicLink :: CanonicalPath -> IO Bool
pathIsSymbolicLink (UnsafeCanonicalPath p) = Directory.pathIsSymbolicLink p

-- | Get the list of directories, canonicalized, between two given paths.
getDirsBetweenRootAndFile :: CanonicalPath -> CanonicalPath -> [CanonicalPath]
getDirsBetweenRootAndFile (UnsafeCanonicalPath rootPath) file =
Expand Down
17 changes: 17 additions & 0 deletions tests/golden/check-symlinks/check-symlinks.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env bats

# SPDX-FileCopyrightText: 2022 Serokell <https://serokell.io>
#
# SPDX-License-Identifier: MPL-2.0

load '../helpers/bats-support/load'
load '../helpers/bats-assert/load'
load '../helpers/bats-file/load'
load '../helpers'


@test "Checking that symlinks are not processed" {
to_temp xrefcheck -v

assert_diff expected.gold
}
Empty file.
11 changes: 11 additions & 0 deletions tests/golden/check-symlinks/dir/b.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!--
- SPDX-FileCopyrightText: 2022 Serokell <https://serokell.io>
-
- SPDX-License-Identifier: MPL-2.0
-->

[Empty file](a)

[Symlink ref](../s.md)

[Symlink ref with anchor](../s.md#a)
20 changes: 20 additions & 0 deletions tests/golden/check-symlinks/expected.gold
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
=== Repository data ===

dir/b.md:
- references:
- reference (relative) at src:7:1-15:
- text: "Empty file"
- link: a
- anchor: -
- reference (relative) at src:9:1-22:
- text: "Symlink ref"
- link: ../s.md
- anchor: -
- reference (relative) at src:11:1-36:
- text: "Symlink ref with anchor"
- link: ../s.md
- anchor: a
- anchors:
none

All repository links are valid.
1 change: 1 addition & 0 deletions tests/golden/check-symlinks/s.md

0 comments on commit 066da26

Please sign in to comment.