Skip to content

Commit a0c5ef8

Browse files
fix(compile): never include the specified output executable in itself (#27877)
1 parent dd1ee58 commit a0c5ef8

File tree

6 files changed

+60
-18
lines changed

6 files changed

+60
-18
lines changed

cli/tools/compile.rs

+17-18
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,18 @@ pub async fn compile(
3737
let binary_writer = factory.create_compile_binary_writer().await?;
3838
let http_client = factory.http_client_provider();
3939
let entrypoint = cli_options.resolve_main_module()?;
40-
let (module_roots, include_files) = get_module_roots_and_include_files(
41-
entrypoint,
42-
&compile_flags,
43-
cli_options.initial_cwd(),
44-
)?;
45-
4640
let output_path = resolve_compile_executable_output_path(
4741
http_client,
4842
&compile_flags,
4943
cli_options.initial_cwd(),
5044
)
5145
.await?;
46+
let (module_roots, include_files) = get_module_roots_and_include_files(
47+
entrypoint,
48+
&url_from_file_path(&cli_options.initial_cwd().join(&output_path))?,
49+
&compile_flags,
50+
cli_options.initial_cwd(),
51+
)?;
5252

5353
let graph = Arc::try_unwrap(
5454
module_graph_creator
@@ -198,6 +198,7 @@ fn validate_output_path(output_path: &Path) -> Result<(), AnyError> {
198198

199199
fn get_module_roots_and_include_files(
200200
entrypoint: &ModuleSpecifier,
201+
output_url: &ModuleSpecifier,
201202
compile_flags: &CompileFlags,
202203
initial_cwd: &Path,
203204
) -> Result<(Vec<ModuleSpecifier>, Vec<ModuleSpecifier>), AnyError> {
@@ -226,9 +227,8 @@ fn get_module_roots_and_include_files(
226227

227228
fn analyze_path(
228229
url: &ModuleSpecifier,
229-
module_roots: &mut Vec<ModuleSpecifier>,
230-
include_files: &mut Vec<ModuleSpecifier>,
231230
searched_paths: &mut HashSet<PathBuf>,
231+
mut add_url: impl FnMut(ModuleSpecifier),
232232
) -> Result<(), AnyError> {
233233
let Ok(path) = url_to_file_path(url) else {
234234
return Ok(());
@@ -240,10 +240,7 @@ fn get_module_roots_and_include_files(
240240
}
241241
if !path.is_dir() {
242242
let url = url_from_file_path(&path)?;
243-
include_files.push(url.clone());
244-
if is_module_graph_module(&url) {
245-
module_roots.push(url);
246-
}
243+
add_url(url);
247244
continue;
248245
}
249246
for entry in std::fs::read_dir(&path).with_context(|| {
@@ -270,12 +267,14 @@ fn get_module_roots_and_include_files(
270267
include_files.push(url);
271268
}
272269
} else {
273-
analyze_path(
274-
&url,
275-
&mut module_roots,
276-
&mut include_files,
277-
&mut searched_paths,
278-
)?;
270+
analyze_path(&url, &mut searched_paths, |file_url| {
271+
if file_url != *output_url {
272+
include_files.push(file_url.clone());
273+
if is_module_graph_module(&file_url) {
274+
module_roots.push(file_url);
275+
}
276+
}
277+
})?;
279278
}
280279
}
281280
Ok((module_roots, include_files))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"tempDir": true,
3+
"steps": [
4+
{
5+
"args": "compile --include . main.ts",
6+
"output": "compile1.out"
7+
},
8+
{
9+
"args": "compile --include . main.ts",
10+
"output": "compile2.out"
11+
}
12+
]
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Check file:[WILDLINE]main.ts
2+
Check file:[WILDLINE]deno.json
3+
Compile file:[WILDLINE]main.ts to [WILDLINE]
4+
5+
Embedded Files
6+
7+
[WILDLINE]
8+
├── deno.json ([WILDLINE])
9+
└── main.ts ([WILDLINE])
10+
11+
Files: [WILDCARD]
12+
Metadata: [WILDCARD]
13+
Remote modules: [WILDCARD]
14+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Compile file:[WILDLINE]main.ts to [WILDLINE]
2+
3+
Embedded Files
4+
5+
[WILDLINE]
6+
├── deno.json ([WILDLINE])
7+
└── main.ts ([WILDLINE])
8+
9+
Files: [WILDCARD]
10+
Metadata: [WILDCARD]
11+
Remote modules: [WILDCARD]
12+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"unstable": ["kv"]
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("WHATEVER");

0 commit comments

Comments
 (0)