Skip to content

Commit acf6312

Browse files
fix(server): env shebang file icon follows the program env runs (#45465)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
1 parent 44fd41b commit acf6312

2 files changed

Lines changed: 37 additions & 2 deletions

File tree

‎apps/server/src/workspace/WorkspaceFileSystem.test.ts‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,13 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceFileSystemLive", (i
151151
const nodeScript =
152152
"#!/opt/python-tools/bin/node --conditions=python\nconsole.log('hello')\n";
153153
yield* writeTextFile(cwd, "run-node", nodeScript);
154+
const envNodeScript =
155+
"#!/usr/bin/env -S LABEL='python script' node --conditions=python\nconsole.log('hello')\n";
156+
yield* writeTextFile(cwd, "run-env-node", envNodeScript);
157+
const envSplitStringScript = "#!/usr/bin/env --split-string=node\nconsole.log('hello')\n";
158+
yield* writeTextFile(cwd, "run-env-split-string", envSplitStringScript);
159+
const envUnsetScript = "#!/usr/bin/env -u FOO node\nconsole.log('hello')\n";
160+
yield* writeTextFile(cwd, "run-env-unset", envUnsetScript);
154161
const image = path.join(cwd, "image");
155162
yield* fs.writeFile(
156163
image,
@@ -162,11 +169,22 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceFileSystemLive", (i
162169
yield* fs.writeFile(archive, emptyZip);
163170
expect(
164171
(yield* service.getMetadata({
165-
paths: [path.join(cwd, "run"), path.join(cwd, "run-node"), image, archive],
172+
paths: [
173+
path.join(cwd, "run"),
174+
path.join(cwd, "run-node"),
175+
path.join(cwd, "run-env-node"),
176+
path.join(cwd, "run-env-split-string"),
177+
path.join(cwd, "run-env-unset"),
178+
image,
179+
archive,
180+
],
166181
})).entries,
167182
).toEqual([
168183
{ kind: "file", byteLength: script.length, mimeType: "text/x-python" },
169184
{ kind: "file", byteLength: nodeScript.length, mimeType: "text/javascript" },
185+
{ kind: "file", byteLength: envNodeScript.length, mimeType: "text/javascript" },
186+
{ kind: "file", byteLength: envSplitStringScript.length, mimeType: "text/javascript" },
187+
{ kind: "file", byteLength: envUnsetScript.length, mimeType: "text/javascript" },
170188
{ kind: "file", byteLength: 8, mimeType: "image/png" },
171189
{ kind: "file", byteLength: 22, mimeType: "application/zip" },
172190
]);

‎apps/server/src/workspace/fileHeaderMimeType.ts‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,24 @@ export function fileHeaderMimeType(bytes: Uint8Array): string | undefined {
1414
if (!shebang) return undefined;
1515
const [, command = "", args = ""] = shebang;
1616
const executable = command.slice(command.lastIndexOf("/") + 1);
17-
const interpreter = executable === "env" ? args : executable;
17+
let interpreter = executable;
18+
if (executable === "env") {
19+
// env runs its first operand; options (some with a separate value) and NAME=value come before it.
20+
// `-S`/`--split-string=` values hold more words, split at unquoted whitespace.
21+
const words = (args.match(/(?:[^\s"']+|"[^"]*"|'[^']*')+/g) ?? []).map((word) =>
22+
word.replace(/["']/g, ""),
23+
);
24+
let index = 0;
25+
while (index < words.length) {
26+
const word = words[index]!;
27+
const splitString = /^(?:-S|--split-string=)(.+)$/.exec(word)?.[1];
28+
if (splitString) words[index] = splitString;
29+
else if (word.startsWith("-") || word.includes("="))
30+
index += /^(?:-[uCP]|--unset|--chdir)$/.test(word) ? 2 : 1;
31+
else break;
32+
}
33+
interpreter = words[index] ?? "";
34+
}
1835
if (/\bpython[\d.]*\b/.test(interpreter)) return "text/x-python";
1936
if (/\b(?:node|nodejs)\b/.test(interpreter)) return "text/javascript";
2037
if (/\bruby\b/.test(interpreter)) return "text/x-ruby";

0 commit comments

Comments
 (0)