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 Dec 30, 2022
1 parent 20f3d05 commit 609282a
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ Unreleased
* [#254](https://github.com/serokell/xrefcheck/pull/254)
+ Now the `dump-config` command does not overwrite a file unless explicitly told with a
`--force` flag. Also, a `--stdout` flag allows to print the config to stdout instead.
the configured Markdown flavour.
* [#250](https://github.com/serokell/xrefcheck/pull/250)
+ Now the redirect behavior for external references can be modified via rules in the
configuration file with the `externalRefRedirects` parameter.
* [#261](https://github.com/serokell/xrefcheck/pull/261)
+ Symlinks are now not processed by the scanner.

0.2.2
==========
Expand Down
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
6 changes: 6 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 All @@ -30,6 +31,7 @@ import Data.Aeson (FromJSON (..), withText)
import Data.Char qualified as C
import Data.List (stripPrefix)
import GHC.IO.Unsafe (unsafePerformIO)
import System.Directory qualified as D
import System.Directory qualified as Directory
import System.Environment (lookupEnv)
import System.FilePath qualified as FP
Expand Down Expand Up @@ -94,6 +96,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) = D.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 609282a

Please sign in to comment.