Merge reviewed PR #3961: fix(project-detect): preserve extras in Python dependency arrays

This commit is contained in:
haelyra committed 2026-11-19 15:14:51 -05:00
commit 9155d4448e
2 files changed
+238 -11

No files matched your search

+102 -11
View File
@@ -184,6 +184,102 @@ 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++;
let valueStart = index;
let value = '';
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) {
value += content.slice(valueStart, index);
index = continuation.lastIndex;
valueStart = index;
continue;
}
}
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: 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. 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 key = /dependencies\s*=\s*\[/y;
let values = null;
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 (character === '"' || character === "'") {
const quoted = readTomlQuotedValue(content, index);
if (!quoted) return [];
if (values !== null) {
values = { value: quoted.value, previous: values, length: values.length + 1 };
}
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 = { length: 0 };
index = key.lastIndex;
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 === ']') {
// 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 [];
}
/**
* Read requirements.txt or pyproject.toml for Python package names
* @param {string} projectDir - Project root directory
@@ -220,17 +316,12 @@ 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]*?)\]/);
if (depMatches) {
const block = depMatches[1];
block.match(/"([^"]+)"/g)?.forEach(m => {
const name = m
.replace(/"/g, '')
.split(/[\s>=<!~@[;]/)[0]
.trim()
.toLowerCase();
if (name) deps.push(name);
});
for (const dependency of readTomlDependencyArray(content)) {
const name = dependency
.split(/[\s>=<!~@[;]/)[0]
.trim()
.toLowerCase();
if (name) deps.push(name);
}
}
} catch {
@@ -0,0 +1,136 @@
const assert = require('assert');
const fs = require('fs');
const os = require('os');
const path = require('path');
const { spawnSync } = require('child_process');
const { getPythonDeps, detectProjectType } = require('../../scripts/lib/project-detect');
let passed = 0;
let failed = 0;
function test(name, source, expected, frameworks) {
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-python-array-'));
try {
fs.writeFileSync(path.join(root, 'pyproject.toml'), source);
assert.deepStrictEqual(getPythonDeps(root), expected);
if (frameworks) assert.deepStrictEqual(detectProjectType(root).frameworks, frameworks);
passed += 1;
console.log(` ✓ ${name}`);
} catch (error) {
failed += 1;
console.log(` ✗ ${name}: ${error.message}`);
} finally {
fs.rmSync(root, { recursive: true, force: true });
}
}
test('retains a framework requirement containing extras',
'[project]\ndependencies = ["fastapi[standard]>=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',
[], []);
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']);
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('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']);
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]); '
+ 'process.stdout.write(JSON.stringify(getPythonDeps(process.argv[2])));';
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(' ✓ unfinished arrays and multiline strings complete without backtracking');
} catch (error) {
failed += 1;
console.log(` ✗ an unfinished array with repeated comments: ${error.message}`);
} finally {
fs.rmSync(unfinishedRoot, { recursive: true, force: true });
}
console.log(`\nPassed: ${passed}, Failed: ${failed}\n`);
process.exitCode = failed > 0 ? 1 : 0;