Skip to content

Commit 5e524ce

Browse files
Fix invalid PEP 723 header in new-script template (#1514)
I was reading PEP723 specs trying to understand the scope for supporting it in envs extension and noticed that the header is invalid in the template, so sending the PR to fix it. ## Summary The script template shipped under `files/templates/new723ScriptTemplate/script.py` opens with `# /// script_name`, which is **not** a valid PEP 723 inline-script metadata header — the [spec](https://packaging.python.org/en/latest/specifications/inline-script-metadata/) mandates the literal type `script`. Compounding this, [newScriptProject.ts](src/features/creators/newScriptProject.ts) substitutes the user's filename into that token at create-time, producing further invalid headers like `# /// my_script`. This PR replaces the template's first line with the literal `# /// script` so the generated script is recognized by every PEP 723–compliant tool, and adds a unit test that locks the behaviour in.
1 parent eea3e13 commit 5e524ce

2 files changed

Lines changed: 45 additions & 1 deletion

File tree

files/templates/new723ScriptTemplate/script.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# /// script_name
1+
# /// script
22
# requires-python = ">=X.XX" TODO: Update this to the minimum Python version you want to support
33
# dependencies = [
44
# TODO: Add any dependencies your script requires
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import assert from 'assert';
2+
import * as fs from 'fs-extra';
3+
import * as os from 'os';
4+
import * as path from 'path';
5+
6+
// Path to the real script template, resolved from the compiled test location
7+
// (out/test/features/creators/ → workspaceRoot/files/templates/...). We do NOT
8+
// rely on `NEW_PROJECT_TEMPLATES_FOLDER` because it is anchored at
9+
// `path.dirname(__dirname)` of the compiled `constants.js`, which resolves to
10+
// `out/` in test mode and does not contain the bundled template tree.
11+
const TEMPLATE_PATH = path.join(
12+
__dirname,
13+
'..',
14+
'..',
15+
'..',
16+
'..',
17+
'files',
18+
'templates',
19+
'new723ScriptTemplate',
20+
'script.py',
21+
);
22+
23+
suite('new723ScriptTemplate / NewScriptProject', () => {
24+
let tmpDir: string;
25+
26+
setup(async () => {
27+
tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'new-script-test-'));
28+
});
29+
30+
teardown(async () => {
31+
await fs.remove(tmpDir);
32+
});
33+
34+
test('Template file starts with a valid PEP 723 header (# /// script)', async () => {
35+
const contents = await fs.readFile(TEMPLATE_PATH, 'utf8');
36+
const firstNonBlankLine = contents.split(/\r?\n/).find((l) => l.trim().length > 0);
37+
38+
assert.strictEqual(
39+
firstNonBlankLine,
40+
'# /// script',
41+
'Template must start with a valid PEP 723 `script` header line',
42+
);
43+
});
44+
});

0 commit comments

Comments
 (0)