From 0a7340cbc5f7d1a82bffbe88bbdc1ada9fa38f2c Mon Sep 17 00:00:00 2001 From: Afloat Date: Tue, 17 Nov 2026 10:34:42 +0800 Subject: [PATCH 1/5] fix(project-detect): preserve extras in Python dependency arrays --- scripts/lib/project-detect.js | 13 +++-- .../lib/project-detect-python-arrays.test.js | 51 +++++++++++++++++++ 2 files changed, 59 insertions(+), 5 deletions(-) create mode 100644 tests/lib/project-detect-python-arrays.test.js diff --git a/scripts/lib/project-detect.js b/scripts/lib/project-detect.js index 9dc392ac1..e1e1f9162 100644 --- a/scripts/lib/project-detect.js +++ b/scripts/lib/project-detect.js @@ -225,17 +225,20 @@ function getPythonDeps(projectDir) { const tomlPath = path.join(projectDir, 'pyproject.toml'); if (fs.existsSync(tomlPath)) { const content = fs.readFileSync(tomlPath, 'utf8'); - const depMatches = content.match(/dependencies\s*=\s*\[([\s\S]*?)\]/); + // Brackets in extras and quoted markers belong to the requirement, not + // the TOML array. Keep strings and comments whole before finding its end. + const depMatches = content.match(/^[ \t]*dependencies[ \t]*=[ \t]*\[((?:[^\]"'#]|"(?:\\.|[^"\\])*"|'[^']*'|#[^\r\n]*)*)\]/m); if (depMatches) { const block = depMatches[1]; - block.match(/"([^"]+)"/g)?.forEach(m => { - const name = m - .replace(/"/g, '') + for (const match of block.matchAll(/#[^\r\n]*|("(?:\\.|[^"\\])*"|'[^']*')/g)) { + if (!match[1]) continue; + const name = match[1] + .slice(1, -1) .split(/[\s>==0.115"]\n', + ['fastapi'], ['fastapi']); +test('retains requirements after a dependency containing extras', + '[project]\ndependencies = ["uvicorn[standard]", "fastapi>=0.115", "flask"]\n', + ['uvicorn', 'fastapi', 'flask'], ['fastapi', 'flask']); +test('reads literal strings and mixed TOML quote styles', + "[project]\ndependencies = ['uvicorn[standard]', \"fastapi\", 'flask~=3.0']\n", + ['uvicorn', 'fastapi', 'flask'], ['fastapi', 'flask']); +test('does not read packages or brackets inside array comments', + '[project]\ndependencies = [\n # "django" ]\n "fastapi", # "flask"\n "uvicorn[standard]"\n]\n', + ['fastapi', 'uvicorn'], ['fastapi']); +test('does not select a commented dependency declaration', + '[project]\n# dependencies = ["django"]\ndependencies = ["flask"]\n', + ['flask'], ['flask']); +test('keeps comment characters within quoted requirements', + '[project]\ndependencies = ["flask @ https://example.invalid/pkg.whl#sha256=abcdef", "fastapi"]\n', + ['flask', 'fastapi'], ['fastapi', 'flask']); +test('keeps escaped quotes in environment markers inside one requirement', + '[project]\ndependencies = ["fastapi; python_version >= \\"3.11\\"", "flask"]\n', + ['fastapi', 'flask'], ['fastapi', 'flask']); +test('does not parse an unterminated dependency array', + '[project]\ndependencies = ["fastapi[standard]"\n', + [], []); + +console.log(`\n=== Results: ${passed} passed, ${failed} failed ===\n`); +process.exitCode = failed > 0 ? 1 : 0; From 87323fb125ce97d8c16059f2ba7d170b05aa4805 Mon Sep 17 00:00:00 2001 From: Afloat Date: Tue, 17 Nov 2026 10:49:07 +0800 Subject: [PATCH 2/5] fix(project-detect): scan unfinished and inline dependency arrays safely --- scripts/lib/project-detect.js | 42 ++++++++++++------- .../lib/project-detect-python-arrays.test.js | 31 +++++++++++++- 2 files changed, 58 insertions(+), 15 deletions(-) diff --git a/scripts/lib/project-detect.js b/scripts/lib/project-detect.js index e1e1f9162..2303c0195 100644 --- a/scripts/lib/project-detect.js +++ b/scripts/lib/project-detect.js @@ -189,6 +189,28 @@ function getPackageJsonDeps(projectDir) { } } +/** + * Find a dependency array while keeping quoted values and comments intact. + * This lightweight scan accepts inline tables too, without backtracking over + * an entire array when an editor has not written its closing bracket yet. + * @param {string} content - pyproject.toml contents + * @returns {string[]} Quoted dependency values, or an empty array if unfinished + */ +function readTomlDependencyArray(content) { + const tokens = content.matchAll(/#[^\r\n]*|"(?:\\.|[^"\\])*"|'[^']*'|\bdependencies\s*=\s*\[|\]/g); + let values = null; + for (const [token] of tokens) { + if (token[0] === '#') continue; + if (values === null) { + if (token.startsWith('dependencies')) values = []; + continue; + } + if (token === ']') return values; + if (token[0] === '"' || token[0] === "'") values.push(token.slice(1, -1)); + } + return []; +} + /** * Read requirements.txt or pyproject.toml for Python package names * @param {string} projectDir - Project root directory @@ -225,20 +247,12 @@ function getPythonDeps(projectDir) { const tomlPath = path.join(projectDir, 'pyproject.toml'); if (fs.existsSync(tomlPath)) { const content = fs.readFileSync(tomlPath, 'utf8'); - // Brackets in extras and quoted markers belong to the requirement, not - // the TOML array. Keep strings and comments whole before finding its end. - const depMatches = content.match(/^[ \t]*dependencies[ \t]*=[ \t]*\[((?:[^\]"'#]|"(?:\\.|[^"\\])*"|'[^']*'|#[^\r\n]*)*)\]/m); - if (depMatches) { - const block = depMatches[1]; - for (const match of block.matchAll(/#[^\r\n]*|("(?:\\.|[^"\\])*"|'[^']*')/g)) { - if (!match[1]) continue; - const name = match[1] - .slice(1, -1) - .split(/[\s>== 0 ? 1 : 0; From f9f3ed5ccfb3c2390c006a3d567e556307b7fbad Mon Sep 17 00:00:00 2001 From: Afloat Date: Tue, 17 Nov 2026 11:00:19 +0800 Subject: [PATCH 3/5] fix(project-detect): preserve TOML key and multiline string boundaries --- scripts/lib/project-detect.js | 66 ++++++++++++++++--- .../lib/project-detect-python-arrays.test.js | 44 ++++++++++--- 2 files changed, 91 insertions(+), 19 deletions(-) diff --git a/scripts/lib/project-detect.js b/scripts/lib/project-detect.js index 2303c0195..e458e2608 100644 --- a/scripts/lib/project-detect.js +++ b/scripts/lib/project-detect.js @@ -189,24 +189,72 @@ function getPackageJsonDeps(projectDir) { } } +/** Read one quoted TOML value, advancing monotonically even if unfinished. */ +function readTomlQuotedValue(content, start) { + const quote = content[start]; + const delimiter = content.startsWith(quote.repeat(3), start) ? quote.repeat(3) : quote; + const multiline = delimiter.length === 3; + let index = start + delimiter.length; + // TOML trims a newline immediately after a multiline opening delimiter. + if (multiline && content.startsWith('\r\n', index)) index += 2; + else if (multiline && content[index] === '\n') index++; + const valueStart = index; + while (index < content.length) { + if (quote === '"' && content[index] === '\\') { + index += 2; + continue; + } + if (content.startsWith(delimiter, index)) { + let end = index + delimiter.length; + // A multiline closing delimiter may be preceded by one or two quotes. + if (multiline) { + while (end < index + 5 && content[end] === quote) end++; + } + return { value: content.slice(valueStart, end - delimiter.length), end }; + } + if (!multiline && (content[index] === '\n' || content[index] === '\r')) return null; + index++; + } + return null; +} + /** * Find a dependency array while keeping quoted values and comments intact. - * This lightweight scan accepts inline tables too, without backtracking over - * an entire array when an editor has not written its closing bracket yet. + * This lightweight scan accepts inline tables too. Its cursor never rewinds, + * including when an editor has not written a closing quote or bracket yet. * @param {string} content - pyproject.toml contents * @returns {string[]} Quoted dependency values, or an empty array if unfinished */ function readTomlDependencyArray(content) { - const tokens = content.matchAll(/#[^\r\n]*|"(?:\\.|[^"\\])*"|'[^']*'|\bdependencies\s*=\s*\[|\]/g); + const key = /dependencies\s*=\s*\[/y; let values = null; - for (const [token] of tokens) { - if (token[0] === '#') continue; - if (values === null) { - if (token.startsWith('dependencies')) values = []; + let index = 0; + while (index < content.length) { + const character = content[index]; + if (character === '#') { + const newline = content.indexOf('\n', index); + if (newline < 0) break; + index = newline + 1; continue; } - if (token === ']') return values; - if (token[0] === '"' || token[0] === "'") values.push(token.slice(1, -1)); + if (character === '"' || character === "'") { + const quoted = readTomlQuotedValue(content, index); + if (!quoted) return []; + if (values !== null) values.push(quoted.value); + index = quoted.end; + continue; + } + if (values === null && character === 'd' && !/[A-Za-z0-9_-]/.test(content[index - 1] || '')) { + key.lastIndex = index; + const match = key.exec(content); + if (match) { + values = []; + index = key.lastIndex; + continue; + } + } + if (values !== null && character === ']') return values; + index++; } return []; } diff --git a/tests/lib/project-detect-python-arrays.test.js b/tests/lib/project-detect-python-arrays.test.js index d63db0a3c..d4464a456 100644 --- a/tests/lib/project-detect-python-arrays.test.js +++ b/tests/lib/project-detect-python-arrays.test.js @@ -53,22 +53,46 @@ test('retains dependencies in a project inline table', test('does not select a dependency declaration inside a quoted value', "description = 'dependencies = [\"django\"]'\n[project]\ndependencies = [\"flask\"]\n", ['flask'], ['flask']); +test('does not match the suffix of a hyphenated dependency key', + '[tool.uv]\ndev-dependencies = ["pytest"]\n[project]\ndependencies = ["flask"]\n', + ['flask'], ['flask']); +test('keeps quotes and fake dependency declarations inside multiline basic descriptions', + '[project]\ndescription = """A 6" display helper.\ndependencies = ["django"]"""\ndependencies = ["flask"]\n', + ['flask'], ['flask']); +test('keeps single quotes inside multiline literal descriptions', + "[project]\ndescription = '''A developer's helper.\ndependencies = [\"django\"]'''\ndependencies = [\"fastapi\"]\n", + ['fastapi'], ['fastapi']); +test('trims the opening newline of multiline quoted requirements', + "[project]\ndependencies = ['''\nfastapi''', \"\"\"\r\nflask\"\"\"]\n", + ['fastapi', 'flask'], ['fastapi', 'flask']); +test('allows one or two quote characters before a multiline closing delimiter', + '[project]\ndescription = """quoted"""""\ndependencies = ["flask"]\n', + ['flask'], ['flask']); +test('an unfinished multiline description never exposes a fake dependency array', + '[project]\ndescription = """A 6" display helper.\ndependencies = ["django"]\n', + [], []); const unfinishedRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-python-unfinished-')); try { - fs.writeFileSync(path.join(unfinishedRoot, 'pyproject.toml'), - '[project]\ndependencies = [\n' + '# abc\n'.repeat(1000)); const script = 'const { getPythonDeps } = require(process.argv[1]); ' + 'process.stdout.write(JSON.stringify(getPythonDeps(process.argv[2])));'; - const result = spawnSync(process.execPath, ['-e', script, - require.resolve('../../scripts/lib/project-detect'), unfinishedRoot], { - encoding: 'utf8', timeout: 5000, - }); - assert.ifError(result.error); - assert.strictEqual(result.status, 0, result.stderr); - assert.deepStrictEqual(JSON.parse(result.stdout), []); + const unfinishedSources = [ + '[project]\ndependencies = [\n' + '# abc\n'.repeat(1000), + '[project]\ndescription = """' + 'line \\" # comment [ ]\n'.repeat(20000), + "[project]\ndescription = '''" + "a developer's # comment [ ]\n".repeat(20000), + ]; + for (const source of unfinishedSources) { + fs.writeFileSync(path.join(unfinishedRoot, 'pyproject.toml'), source); + const result = spawnSync(process.execPath, ['-e', script, + require.resolve('../../scripts/lib/project-detect'), unfinishedRoot], { + encoding: 'utf8', timeout: 5000, + }); + assert.ifError(result.error); + assert.strictEqual(result.status, 0, result.stderr); + assert.deepStrictEqual(JSON.parse(result.stdout), []); + } passed += 1; - console.log(' ✓ an unfinished array with repeated comments completes without backtracking'); + console.log(' ✓ unfinished arrays and multiline strings complete without backtracking'); } catch (error) { failed += 1; console.log(` ✗ an unfinished array with repeated comments: ${error.message}`); From a796cab879c05a054a8a33c6572549a3c499c8ca Mon Sep 17 00:00:00 2001 From: Afloat Date: Tue, 17 Nov 2026 11:23:16 +0800 Subject: [PATCH 4/5] fix(project-detect): retain multiline continuations and array boundaries --- scripts/lib/project-detect.js | 20 +++++++++++++++++-- .../lib/project-detect-python-arrays.test.js | 9 +++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/scripts/lib/project-detect.js b/scripts/lib/project-detect.js index e458e2608..7be49caa8 100644 --- a/scripts/lib/project-detect.js +++ b/scripts/lib/project-detect.js @@ -198,9 +198,21 @@ function readTomlQuotedValue(content, start) { // TOML trims a newline immediately after a multiline opening delimiter. if (multiline && content.startsWith('\r\n', index)) index += 2; else if (multiline && content[index] === '\n') index++; - const valueStart = index; + let valueStart = index; + const parts = []; + const continuation = /\\[ \t]*\r?\n[ \t\r\n]*/y; while (index < content.length) { if (quote === '"' && content[index] === '\\') { + if (multiline) { + continuation.lastIndex = index; + const match = continuation.exec(content); + if (match) { + parts.push(content.slice(valueStart, index)); + index = continuation.lastIndex; + valueStart = index; + continue; + } + } index += 2; continue; } @@ -210,7 +222,8 @@ function readTomlQuotedValue(content, start) { if (multiline) { while (end < index + 5 && content[end] === quote) end++; } - return { value: content.slice(valueStart, end - delimiter.length), end }; + parts.push(content.slice(valueStart, end - delimiter.length)); + return { value: parts.join(''), end }; } if (!multiline && (content[index] === '\n' || content[index] === '\r')) return null; index++; @@ -253,6 +266,9 @@ function readTomlDependencyArray(content) { continue; } } + // Dependency values are strings; an unquoted opening bracket here can be + // a later table header while the dependency array is still unfinished. + if (values !== null && character === '[') return []; if (values !== null && character === ']') return values; index++; } diff --git a/tests/lib/project-detect-python-arrays.test.js b/tests/lib/project-detect-python-arrays.test.js index d4464a456..5d49061a9 100644 --- a/tests/lib/project-detect-python-arrays.test.js +++ b/tests/lib/project-detect-python-arrays.test.js @@ -47,6 +47,9 @@ test('keeps escaped quotes in environment markers inside one requirement', test('does not parse an unterminated dependency array', '[project]\ndependencies = ["fastapi[standard]"\n', [], []); +test('does not use a later table header to close an unfinished dependency array', + '[project]\ndependencies = ["fastapi",\n[tool.demo]\n', + [], []); test('retains dependencies in a project inline table', 'project = { name = "demo", version = "1.0", dependencies = ["fastapi[standard]", "flask"] }\n', ['fastapi', 'flask'], ['fastapi', 'flask']); @@ -65,6 +68,12 @@ test('keeps single quotes inside multiline literal descriptions', test('trims the opening newline of multiline quoted requirements', "[project]\ndependencies = ['''\nfastapi''', \"\"\"\r\nflask\"\"\"]\n", ['fastapi', 'flask'], ['fastapi', 'flask']); +test('joins escaped newlines inside multiline basic requirements', + '[project]\ndependencies = ["""fast\\\n api""", """fl\\ \t\r\n\n ask"""]\n', + ['fastapi', 'flask'], ['fastapi', 'flask']); +test('leaves multiline literal backslashes unchanged', + "[project]\ndependencies = ['''fast\\\napi''']\n", + ['fast\\'], []); test('allows one or two quote characters before a multiline closing delimiter', '[project]\ndescription = """quoted"""""\ndependencies = ["flask"]\n', ['flask'], ['flask']); From ec6237570b6f149f93150247555f6b44de146f5a Mon Sep 17 00:00:00 2001 From: Afloat Date: Thu, 19 Nov 2026 10:43:56 +0800 Subject: [PATCH 5/5] refactor: collect Python dependencies without mutation Signed-off-by: Afloat --- scripts/lib/project-detect.js | 24 +++++++++++++------ .../lib/project-detect-python-arrays.test.js | 23 ++++++++++++++++++ 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/scripts/lib/project-detect.js b/scripts/lib/project-detect.js index 7be49caa8..7c51aa0a6 100644 --- a/scripts/lib/project-detect.js +++ b/scripts/lib/project-detect.js @@ -199,7 +199,7 @@ function readTomlQuotedValue(content, start) { if (multiline && content.startsWith('\r\n', index)) index += 2; else if (multiline && content[index] === '\n') index++; let valueStart = index; - const parts = []; + let value = ''; const continuation = /\\[ \t]*\r?\n[ \t\r\n]*/y; while (index < content.length) { if (quote === '"' && content[index] === '\\') { @@ -207,7 +207,7 @@ function readTomlQuotedValue(content, start) { continuation.lastIndex = index; const match = continuation.exec(content); if (match) { - parts.push(content.slice(valueStart, index)); + value += content.slice(valueStart, index); index = continuation.lastIndex; valueStart = index; continue; @@ -222,8 +222,7 @@ function readTomlQuotedValue(content, start) { if (multiline) { while (end < index + 5 && content[end] === quote) end++; } - parts.push(content.slice(valueStart, end - delimiter.length)); - return { value: parts.join(''), end }; + return { value: value + content.slice(valueStart, end - delimiter.length), end }; } if (!multiline && (content[index] === '\n' || content[index] === '\r')) return null; index++; @@ -253,7 +252,9 @@ function readTomlDependencyArray(content) { if (character === '"' || character === "'") { const quoted = readTomlQuotedValue(content, index); if (!quoted) return []; - if (values !== null) values.push(quoted.value); + if (values !== null) { + values = { value: quoted.value, previous: values, length: values.length + 1 }; + } index = quoted.end; continue; } @@ -261,7 +262,7 @@ function readTomlDependencyArray(content) { key.lastIndex = index; const match = key.exec(content); if (match) { - values = []; + values = { length: 0 }; index = key.lastIndex; continue; } @@ -269,7 +270,16 @@ function readTomlDependencyArray(content) { // Dependency values are strings; an unquoted opening bracket here can be // a later table header while the dependency array is still unfinished. if (values !== null && character === '[') return []; - if (values !== null && character === ']') return values; + if (values !== null && character === ']') { + // Prepending immutable nodes keeps collection linear; restore source order once. + let current = values; + const reversed = Array.from({ length: values.length }, () => { + const value = current.value; + current = current.previous; + return value; + }); + return reversed.map((_, offset) => reversed[reversed.length - 1 - offset]); + } index++; } return []; diff --git a/tests/lib/project-detect-python-arrays.test.js b/tests/lib/project-detect-python-arrays.test.js index 5d49061a9..6c53e07a1 100644 --- a/tests/lib/project-detect-python-arrays.test.js +++ b/tests/lib/project-detect-python-arrays.test.js @@ -81,6 +81,29 @@ test('an unfinished multiline description never exposes a fake dependency array' '[project]\ndescription = """A 6" display helper.\ndependencies = ["django"]\n', [], []); +const largeRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-python-large-array-')); +try { + const expected = Array.from({ length: 50000 }, (_, index) => `dependency-${index}`); + fs.writeFileSync(path.join(largeRoot, 'pyproject.toml'), + `[project]\ndependencies = ${JSON.stringify(expected)}\n`); + const script = 'const { getPythonDeps } = require(process.argv[1]); ' + + 'process.stdout.write(JSON.stringify(getPythonDeps(process.argv[2])));'; + const result = spawnSync(process.execPath, ['-e', script, + require.resolve('../../scripts/lib/project-detect'), largeRoot], { + encoding: 'utf8', timeout: 5000, maxBuffer: 2 * 1024 * 1024, + }); + assert.ifError(result.error); + assert.strictEqual(result.status, 0, result.stderr); + assert.deepStrictEqual(JSON.parse(result.stdout), expected); + passed += 1; + console.log(' ✓ large dependency arrays preserve source order without repeated copying'); +} catch (error) { + failed += 1; + console.log(` ✗ large dependency arrays: ${error.message}`); +} finally { + fs.rmSync(largeRoot, { recursive: true, force: true }); +} + const unfinishedRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-python-unfinished-')); try { const script = 'const { getPythonDeps } = require(process.argv[1]); '