Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(cli): do not scan through symlinks into dependency or VCS trees (#7994)

EJ Hammond384f1851b504
diff --git a/.changeset/codemod-walk-skip-symlinked-dependencies.md b/.changeset/codemod-walk-skip-symlinked-dependencies.md
new file mode 100644
index 0000000000..8bea5797c0
--- /dev/null
+++ b/.changeset/codemod-walk-skip-symlinked-dependencies.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/cli': patch
+---
+
+[fix] `astryx upgrade` no longer scans through a symbolic link into installed dependencies or version-control metadata, such as a symlinked `node_modules` or a link into a package store. Those trees were already never written; skipping them stops a run from the project root spending minutes reading them. Other symbolically linked folders are still scanned so their required changes are reported.
+
+@ejhammond
diff --git a/packages/cli/assets/codemods/__tests__/runner.symlinks.test.mjs b/packages/cli/assets/codemods/__tests__/runner.symlinks.test.mjs
new file mode 100644
index 0000000000..1d9950db82
--- /dev/null
+++ b/packages/cli/assets/codemods/__tests__/runner.symlinks.test.mjs
@@ -0,0 +1,205 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file The codemod walkers never follow a symbolic link into installed
+ * dependencies, a package store or version-control metadata; other linked
+ * folders are still scanned, read-only.
+ */
+
+import {afterEach, beforeEach, describe, expect, it} from 'vitest';
+import * as fs from 'node:fs';
+import * as path from 'node:path';
+import {runCodemods} from '../runner.mjs';
+import {findSourceFiles as findIntegrationSourceFiles} from '../run-codemod.mjs';
+
+let tmpDir;
+let originalCwd;
+
+beforeEach(() => {
+ originalCwd = process.cwd();
+ tmpDir = fs.mkdtempSync(path.join(process.cwd(), '.astryx-runner-test-'));
+ process.chdir(tmpDir);
+});
+
+afterEach(() => {
+ process.chdir(originalCwd);
+ fs.rmSync(tmpDir, {recursive: true, force: true});
+});
+
+describe('findSourceFiles — symbolic links into dependency trees', () => {
+ const manifests = [
+ {
+ version: '0.1.3',
+ transforms: [
+ {
+ name: 'p',
+ meta: {title: 'p'},
+ transform: f => f.source.replace(/foo/g, 'bar'),
+ },
+ ],
+ },
+ ];
+
+ // A tree a symlinked dependency folder points at: a package store outside
+ // the project, holding a file the transform would change.
+ function seedStore() {
+ const store = path.join(tmpDir, 'store', 'node_modules', 'pkg');
+ fs.mkdirSync(store, {recursive: true});
+ fs.writeFileSync(path.join(store, 'index.ts'), 'const foo = 1;\n');
+ fs.mkdirSync(path.join(tmpDir, 'src'), {recursive: true});
+ fs.writeFileSync(path.join(tmpDir, 'src', 'real.ts'), 'const foo = 2;\n');
+ return store;
+ }
+
+ it('does not descend a symlinked node_modules', async () => {
+ const store = seedStore();
+ fs.symlinkSync(
+ path.dirname(store),
+ path.join(tmpDir, 'src', 'node_modules'),
+ );
+
+ const r = await runCodemods(manifests, {
+ apply: true,
+ path: './src',
+ root: tmpDir,
+ silent: true,
+ });
+
+ expect(r.writtenFiles.map(f => path.basename(f))).toEqual(['real.ts']);
+ expect(r.protectedFiles).toEqual([]);
+ expect(
+ findIntegrationSourceFiles(path.join(tmpDir, 'src')).map(f =>
+ path.relative(tmpDir, f),
+ ),
+ ).toEqual([path.join('src', 'real.ts')]);
+ });
+
+ it('does not descend a link whose target is inside a dependency tree', async () => {
+ const store = seedStore();
+ fs.symlinkSync(store, path.join(tmpDir, 'src', 'pkg'));
+
+ const r = await runCodemods(manifests, {
+ apply: true,
+ path: './src',
+ root: tmpDir,
+ silent: true,
+ });
+
+ expect(r.writtenFiles.map(f => path.basename(f))).toEqual(['real.ts']);
+ expect(r.protectedFiles).toEqual([]);
+ expect(
+ findIntegrationSourceFiles(path.join(tmpDir, 'src')).map(f =>
+ path.relative(tmpDir, f),
+ ),
+ ).toEqual([path.join('src', 'real.ts')]);
+ });
+
+ it('does not descend a link into a Yarn cache folder', async () => {
+ const cached = path.join(tmpDir, '.yarn', 'cache', 'pkg');
+ fs.mkdirSync(cached, {recursive: true});
+ fs.writeFileSync(path.join(cached, 'index.ts'), 'const foo = 1;\n');
+ fs.mkdirSync(path.join(tmpDir, 'src'), {recursive: true});
+ fs.writeFileSync(path.join(tmpDir, 'src', 'real.ts'), 'const foo = 2;\n');
+ fs.symlinkSync(cached, path.join(tmpDir, 'src', 'pkg'));
+
+ const r = await runCodemods(manifests, {
+ apply: true,
+ path: '.',
+ root: tmpDir,
+ silent: true,
+ });
+
+ expect(r.writtenFiles.map(f => path.basename(f))).toEqual(['real.ts']);
+ expect(r.protectedFiles).toEqual([]);
+ expect(
+ findIntegrationSourceFiles(tmpDir).map(f => path.relative(tmpDir, f)),
+ ).toEqual([path.join('src', 'real.ts')]);
+ });
+
+ it('still descends ordinary symlinked directories in a project below a node_modules folder', async () => {
+ const project = path.join(tmpDir, 'node_modules', 'consumer');
+ const shared = path.join(project, 'shared');
+ fs.mkdirSync(path.join(project, 'src'), {recursive: true});
+ fs.mkdirSync(shared, {recursive: true});
+ fs.writeFileSync(path.join(shared, 'nested.ts'), 'const foo = 1;\n');
+ fs.symlinkSync(shared, path.join(project, 'src', 'linked'));
+
+ const r = await runCodemods(manifests, {
+ apply: true,
+ path: path.join(project, 'src'),
+ root: project,
+ silent: true,
+ });
+
+ expect(r.protectedFiles).toEqual([
+ expect.objectContaining({
+ file: 'src/linked/nested.ts',
+ reason: 'symlink',
+ }),
+ ]);
+ expect(
+ findIntegrationSourceFiles(path.join(project, 'src'), project).map(f =>
+ path.relative(project, f),
+ ),
+ ).toEqual([path.join('src', 'linked', 'nested.ts')]);
+ });
+
+ it('does not descend a link into a Yarn cache outside the project', async () => {
+ const project = path.join(tmpDir, 'project');
+ const cached = path.join(tmpDir, 'other-project', '.yarn', 'cache', 'pkg');
+ fs.mkdirSync(cached, {recursive: true});
+ fs.writeFileSync(path.join(cached, 'index.ts'), 'const foo = 1;\n');
+ fs.mkdirSync(path.join(project, 'src'), {recursive: true});
+ fs.writeFileSync(path.join(project, 'src', 'real.ts'), 'const foo = 2;\n');
+ fs.symlinkSync(
+ path.join('..', '..', 'other-project', '.yarn', 'cache', 'pkg'),
+ path.join(project, 'src', 'pkg'),
+ );
+
+ const r = await runCodemods(manifests, {
+ apply: true,
+ path: path.join(project, 'src'),
+ root: project,
+ silent: true,
+ });
+
+ expect(r.writtenFiles.map(f => path.basename(f))).toEqual(['real.ts']);
+ expect(r.protectedFiles).toEqual([]);
+ expect(
+ findIntegrationSourceFiles(path.join(project, 'src')).map(f =>
+ path.relative(project, f),
+ ),
+ ).toEqual([path.join('src', 'real.ts')]);
+ expect(fs.readFileSync(path.join(cached, 'index.ts'), 'utf-8')).toBe(
+ 'const foo = 1;\n',
+ );
+ });
+
+ it('does not descend a link from a project below node_modules to a sibling dependency', async () => {
+ const project = path.join(tmpDir, 'node_modules', 'consumer');
+ const sibling = path.join(tmpDir, 'node_modules', 'dep');
+ fs.mkdirSync(path.join(project, 'src'), {recursive: true});
+ fs.mkdirSync(sibling, {recursive: true});
+ fs.writeFileSync(path.join(sibling, 'index.ts'), 'const foo = 1;\n');
+ fs.writeFileSync(path.join(project, 'src', 'real.ts'), 'const foo = 2;\n');
+ fs.symlinkSync(sibling, path.join(project, 'src', 'dep'));
+
+ const r = await runCodemods(manifests, {
+ apply: true,
+ path: path.join(project, 'src'),
+ root: project,
+ silent: true,
+ });
+
+ expect(r.writtenFiles.map(f => path.basename(f))).toEqual(['real.ts']);
+ expect(r.protectedFiles).toEqual([]);
+ expect(
+ findIntegrationSourceFiles(path.join(project, 'src'), project).map(f =>
+ path.relative(project, f),
+ ),
+ ).toEqual([path.join('src', 'real.ts')]);
+ expect(fs.readFileSync(path.join(sibling, 'index.ts'), 'utf-8')).toBe(
+ 'const foo = 1;\n',
+ );
+ });
+});
diff --git a/packages/cli/assets/codemods/integration-runner.mjs b/packages/cli/assets/codemods/integration-runner.mjs
index 3486fa5f11..f0379b6152 100644
--- a/packages/cli/assets/codemods/integration-runner.mjs
+++ b/packages/cli/assets/codemods/integration-runner.mjs
@@ -125,7 +125,7 @@ export function runIntegrationCodemods(
if (codeEntries.length > 0) {
const resolvedPath = path.resolve(srcPath);
const files = fs.existsSync(resolvedPath)
- ? findSourceFiles(resolvedPath)
+ ? findSourceFiles(resolvedPath, path.resolve(root))
: [];
for (const entry of codeEntries) {
if (apply && writtenFiles.length !== protectionWriteCount) {
diff --git a/packages/cli/assets/codemods/run-codemod.mjs b/packages/cli/assets/codemods/run-codemod.mjs
index 71c4b209bb..d7368d63f0 100644
--- a/packages/cli/assets/codemods/run-codemod.mjs
+++ b/packages/cli/assets/codemods/run-codemod.mjs
@@ -35,6 +35,7 @@ import {
fixDirectiveCorruption,
validateOutput,
isIgnoredDirectory,
+ isIgnoredLinkedDirectory,
} from './runner.mjs';
import {createFileProtectionResolver} from '../../foundation/fs/file-protection.mjs';
@@ -86,9 +87,10 @@ function logProtected(log, protections) {
/**
* Recursively find candidate source files in a directory.
* @param {string} dir
+ * @param {string} [root] the operation's project root (default: `dir`)
* @returns {string[]}
*/
-export function findSourceFiles(dir) {
+export function findSourceFiles(dir, root = dir) {
/** @type {string[]} */
const results = [];
/** @param {string} currentDir @param {Set<string>} [ancestors] */
@@ -111,12 +113,24 @@ export function findSourceFiles(dir) {
for (const entry of entries) {
const fullPath = path.join(currentDir, entry.name);
// Symlinked files and descendants remain read-only candidates so a
- // required change can be reported. Real-path ancestors prevent cycles.
+ // required change can be reported. Real-path ancestors prevent cycles,
+ // and a link into a dependency or VCS tree is not descended.
if (entry.isSymbolicLink()) {
try {
const target = fs.statSync(fullPath);
if (target.isFile()) results.push(fullPath);
- else if (target.isDirectory()) walk(fullPath, nextAncestors);
+ else if (
+ target.isDirectory() &&
+ !isIgnoredLinkedDirectory(
+ dir,
+ fullPath,
+ entry.name,
+ fs.realpathSync(fullPath),
+ root,
+ )
+ ) {
+ walk(fullPath, nextAncestors);
+ }
} catch {
// Broken links have no source bytes to evaluate.
}
diff --git a/packages/cli/assets/codemods/runner.mjs b/packages/cli/assets/codemods/runner.mjs
index 1098147d58..1489a68505 100644
--- a/packages/cli/assets/codemods/runner.mjs
+++ b/packages/cli/assets/codemods/runner.mjs
@@ -61,22 +61,85 @@ const IGNORED_DIRS = new Set([
]);
export {IGNORED_DIRS};
+/** Yarn's install and cache folders, a hard boundary under `.yarn`. */
+const YARN_STORE_DIRS = new Set(['__virtual__', 'cache', 'sdks', 'unplugged']);
+
+/**
+ * The number of path segments a hard boundary starting at `parts[index]`
+ * spans: 1 for a name in {@link IGNORED_DIRS}, 2 for `.yarn` followed by one
+ * of its store folders, 0 when none starts there.
+ * @param {string[]} parts
+ * @param {number} index
+ */
+function boundaryLength(parts, index) {
+ if (IGNORED_DIRS.has(parts[index])) return 1;
+ if (parts[index] === '.yarn' && YARN_STORE_DIRS.has(parts[index + 1])) {
+ return 2;
+ }
+ return 0;
+}
+
/** @param {string} root @param {string} fullPath @param {string} name */
export function isIgnoredDirectory(root, fullPath, name) {
if (IGNORED_DIRS.has(name)) return true;
const parts = path.relative(root, fullPath).split(path.sep);
- return (
- parts[0] === '.yarn' &&
- ['__virtual__', 'cache', 'sdks', 'unplugged'].includes(parts[1])
- );
+ return boundaryLength(parts, 0) === 2;
+}
+
+/**
+ * Whether a symbolic link to a directory leads into a tree the scan never
+ * descends. The link is checked like a directory by {@link isIgnoredDirectory},
+ * and its resolved target's absolute path is searched for the same hard
+ * boundaries, wherever the target lives: a symlinked `node_modules`, or a link
+ * into a dependency, package-store or version-control tree such as another
+ * project's `.yarn/cache`, is not walked. Only a target that stays inside the
+ * operation root may sit below a boundary that also contains that root, so a
+ * project that itself lives below a `node_modules` folder is still scanned; a
+ * link from it to a sibling package under that same folder is not.
+ * @param {string} root scan root
+ * @param {string} fullPath the link's path
+ * @param {string} name the link's name
+ * @param {string} realTarget the link's resolved real path
+ * @param {string} [operationRoot] the project root of the operation (default: the scan root)
+ * @returns {boolean}
+ */
+export function isIgnoredLinkedDirectory(
+ root,
+ fullPath,
+ name,
+ realTarget,
+ operationRoot = root,
+) {
+ if (isIgnoredDirectory(root, fullPath, name)) return true;
+ let realRoot = operationRoot;
+ try {
+ realRoot = fs.realpathSync(operationRoot);
+ } catch {
+ // An unreadable root has no descendants to compare against.
+ }
+ const relative = path.relative(realRoot, realTarget);
+ const inside =
+ relative === '' ||
+ (relative !== '..' &&
+ !relative.startsWith(`..${path.sep}`) &&
+ !path.isAbsolute(relative));
+ // Inside the root, only a boundary that ends below the root counts.
+ const floor = inside ? realRoot.split(path.sep).length : 0;
+ const targetParts = realTarget.split(path.sep);
+ for (let index = 0; index < targetParts.length; index++) {
+ const length = boundaryLength(targetParts, index);
+ if (length > 0 && index + length > floor) return true;
+ }
+ return false;
}
/**
* Recursively find all source files in a directory.
* @param {string} dir
+ * @param {string} [root] the operation's project root (default: `dir`)
* @returns {string[]}
*/
-function findSourceFiles(dir) {
+function findSourceFiles(dir, root = dir) {
/** @type {string[]} */
const results = [];
const extensions = new Set([
@@ -112,13 +175,23 @@ function findSourceFiles(dir) {
for (const entry of entries) {
const fullPath = path.join(currentDir, entry.name);
// Symlinked files and descendants remain read-only candidates so a
- // required change can be reported. Real-path ancestors prevent cycles.
+ // required change can be reported. Real-path ancestors prevent cycles,
+ // and a link into a dependency or VCS tree is not descended.
if (entry.isSymbolicLink()) {
try {
const target = fs.statSync(fullPath);
if (target.isFile() && extensions.has(path.extname(entry.name))) {
results.push(fullPath);
- } else if (target.isDirectory()) {
+ } else if (
+ target.isDirectory() &&
+ !isIgnoredLinkedDirectory(
+ dir,
+ fullPath,
+ entry.name,
+ fs.realpathSync(fullPath),
+ root,
+ )
+ ) {
walk(fullPath, nextAncestors);
}
} catch {
@@ -472,7 +545,7 @@ export async function runCodemods(
let files = [];
if (sourcePathExists) {
log.step(`Scanning ${resolvedPath} for source files...`);
- files = findSourceFiles(resolvedPath);
+ files = findSourceFiles(resolvedPath, path.resolve(root));
if (files.length === 0) {
log.warn('No source files found.');
} else {
diff --git a/packages/cli/clients/cli/commands/upgrade.doc.mjs b/packages/cli/clients/cli/commands/upgrade.doc.mjs
index 1ee6568186..587f64c2d3 100644
--- a/packages/cli/clients/cli/commands/upgrade.doc.mjs
+++ b/packages/cli/clients/cli/commands/upgrade.doc.mjs
@@ -157,6 +157,13 @@ export const doc = {
'it is an installed dependency (such as anything in `node_modules`), is inside `.git`, `.hg`, or `.sl`, is a symbolic link, or is outside the project',
],
},
+ {
+ type: 'prose',
+ text:
+ 'upgrade does not scan installed dependencies or version-control folders at all, including through a symbolic link: ' +
+ 'a symlinked `node_modules`, or a link whose target is inside one or inside a package store such as `.yarn/cache`, is skipped. ' +
+ 'Other symbolically linked folders are scanned so a change they need is reported, but nothing is written through them.',
+ },
{
type: 'prose',
text: