diff --git a/changelog.d/fix-design-lint-missing-paths-through-symlinks.md b/changelog.d/fix-design-lint-missing-paths-through-symlinks.md new file mode 100644 index 000000000..67af7b8eb --- /dev/null +++ b/changelog.d/fix-design-lint-missing-paths-through-symlinks.md @@ -0,0 +1 @@ +`gstack-design-lint` now names a path it cannot read relative to the folder you ran it from, even when you reach that path through a symlink. Before, a missing file, a dangling link or a link that points at itself under a symlinked folder (on macOS, anything under `/tmp` or `/var`) printed its full absolute path. The design-lint tests also pass on a Mac again, where four of them compared paths against the unresolved temp folder. Thanks to Akkyman2 for the report and first fix (#4856). diff --git a/lib/design-lint/static.ts b/lib/design-lint/static.ts index 7824ee2c0..e960dcbd8 100644 --- a/lib/design-lint/static.ts +++ b/lib/design-lint/static.ts @@ -54,7 +54,7 @@ export function collectFiles(paths: string[], warn: (l: string) => void = () => const visit = (p: string, top: boolean) => { let abs = path.resolve(p); let st: fs.Stats; - try { abs = fs.realpathSync(abs); st = fs.statSync(abs); } catch { warn(`design-lint: cannot read ${p}`); if (top) skipped.push({ file: abs, kind: 'unreadable', why: 'it cannot be read' }); return; } + try { abs = fs.realpathSync(abs); st = fs.statSync(abs); } catch { warn(`design-lint: cannot read ${p}`); if (top) skipped.push({ file: realDir(abs), kind: 'unreadable', why: 'it cannot be read' }); return; } if (seen.has(abs)) return; if (st.isDirectory()) { seen.add(abs); @@ -343,8 +343,15 @@ export function lintStaticRaw(paths: string[], opts: StaticOptions = {}): RawSta return { files, contexts, raw, skipped: skippedReporter.findings }; } -/** A directory's real path, so a cwd or lint root reached through a symlink still matches the files collected under it. */ -export const realDir = (p: string): string => { try { return fs.realpathSync(p); } catch { return path.resolve(p); } }; +/** A path's real path, so a cwd or lint root reached through a symlink still matches the files collected under it; a path that cannot be resolved (missing, a dangling or looping link, no permission) keeps its unresolved tail under the real path of its nearest resolvable ancestor. */ +export const realDir = (p: string): string => { + const tail: string[] = []; + for (let dir = path.resolve(p); ; dir = path.dirname(dir)) { + try { return path.join(fs.realpathSync(dir), ...tail.reverse()); } catch {} + if (path.dirname(dir) === dir) return path.resolve(p); + tail.push(path.basename(dir)); + } +}; export function lintStatic(paths: string[], opts: StaticOptions = {}): StaticResult { const cwd = realDir(opts.cwd ?? process.cwd()); diff --git a/test/design-lint-fifth-cycle.test.ts b/test/design-lint-fifth-cycle.test.ts index 730a5edd6..c333cfdf0 100644 --- a/test/design-lint-fifth-cycle.test.ts +++ b/test/design-lint-fifth-cycle.test.ts @@ -87,11 +87,18 @@ describe('design-lint fifth cycle: dead markup and hostile inputs', () => { expect(resolveRef('../outside.css', path.join(dir, 'site'), path.join(dir, 'site'))).toBeNull(); }); - test('an unreadable top-level path beside a readable page is one internal-error finding with its own fix', () => { + // Value: protects=an unreadable top-level path prints relative to a cwd reached through a symlink; fails_when=the finding keeps the unresolved path while cwd is resolved; why_new=only a symlinked tmpdir (macOS) hit it; seam=none + test('an unreadable top-level path beside a readable page is one internal-error finding with its own fix, also through a symlink', () => { const dir = tree({ 'index.html': page('', '

Hi

Body.

') }); const r = lintStatic([dir, path.join(dir, 'nowhere.html')], { cwd: dir, ...quiet }); expect(r.files.map((f) => path.basename(f))).toEqual(['index.html']); expect(only(r.findings, 'internal-error').map((f) => [f.grade, f.file, f.line, /cannot be read/.test(f.evidence), f.fix])).toEqual([['ask', 'nowhere.html', 1, true, 'check the path']]); + const link = dir + '-link'; + fs.symlinkSync(dir, link); + tmpDirs.push(link); + fs.symlinkSync(path.join(dir, 'self'), path.join(dir, 'self')); + const via = lintStatic([link, path.join(link, 'gone', 'nowhere.html'), path.join(link, 'self')], { cwd: link, ...quiet }); + expect(only(via.findings, 'internal-error').map((f) => f.file)).toEqual([path.join('gone', 'nowhere.html'), 'self']); }); }); diff --git a/test/design-lint.test.ts b/test/design-lint.test.ts index dae2d773d..cbe35994c 100644 --- a/test/design-lint.test.ts +++ b/test/design-lint.test.ts @@ -43,9 +43,9 @@ function tmpPage(html: string, name = 'index.html'): string { return file; } -/** A temporary tree: each key is a relative path (parent directories are created), each value the file's text. */ +/** A temporary tree: each key is a relative path (parent directories are created), each value the file's text. Its root is the real path, as the lint walks real paths. */ function tmpTree(files: Record): string { - const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'design-lint-tree-')); + const dir = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), 'design-lint-tree-'))); tmpDirs.push(dir); for (const [rel, text] of Object.entries(files)) { fs.mkdirSync(path.dirname(path.join(dir, rel)), { recursive: true });