denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
97.92k stars 5.39k forks source link

Deno compile: dynamic imports using `expandGlob` not included in compiled executable #26877

Closed Renotje6 closed 3 days ago

Renotje6 commented 4 days ago

Description: I'm attempting to dynamically import files based on a glob pattern using the expandGlob function from @std/fs. The code functions as expected during development, but when I compile the script into an executable, the files matching the glob pattern are not included.

Here's a the function:

import { expandGlob } from '@std/fs';

/**
 * Dynamically import all files matching the glob pattern
 * @param globPath The glob pattern to match files
 */
export async function importer(globPath: string) {
    for await (const file of expandGlob(globPath)) {
        if (file.isFile) {
            await import(`file://${file.path}`);
        }
    }
}

Steps to Reproduce

  1. Use the above code to load files dynamically based on a glob pattern.
  2. Compile the script using deno compile --target x86_64-pc-windows-msvc src/main.ts (change target based on os)
  3. Run the compiled executable.

Expected Behavior

The executable should include all files matching the glob pattern so they can be dynamically imported at runtime.

Actual Behavior

The files matching the glob pattern are not included in the compiled executable, resulting in import errors at runtime. However, if I use static imports, the files are correctly included in the executable.

Additional Information

Deno Version: 2.0.6
@std/fs version: 1.0.4

Using static strings works as expected, so this issue seems specific to dynamic imports with expandGlob. I've tried using the --include flag to specify matching files, but it still does not resolve the issue.

Expected Resolution Support for dynamic imports with glob patterns in compiled executables, or documentation on correctly including files matched by expandGlob.

dsherret commented 3 days ago

This is basically impossible to statically analyze for properly. You need to use the --include flag to include the files you need for deno compile. https://docs.deno.com/runtime/reference/cli/compiler/#compile-options-include

If you're having an issue with the --include flag, then please open a new issue with a reproduction.