Skip to content

Commit

Permalink
Fixed compilation issues with no outdir (#14717)
Browse files Browse the repository at this point in the history
Co-authored-by: Meghan Denny <meghan@bun.sh>
  • Loading branch information
BjornTheProgrammer and nektro authored Oct 26, 2024
1 parent b895738 commit 9e317c7
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/bundler/bundle_v2.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11739,8 +11739,9 @@ pub const LinkerContext = struct {
) catch unreachable;

const root_path = c.resolver.opts.output_dir;
const more_than_one_output = c.parse_graph.additional_output_files.items.len > 0 or c.parse_graph.generate_bytecode_cache or (has_css_chunk and has_js_chunk);

if (root_path.len == 0 and (c.parse_graph.additional_output_files.items.len > 0 or c.parse_graph.generate_bytecode_cache or (has_css_chunk and has_js_chunk)) and !c.resolver.opts.compile) {
if (!c.resolver.opts.compile and more_than_one_output and !c.resolver.opts.supports_multiple_outputs) {
try c.log.addError(null, Logger.Loc.Empty, "cannot write multiple output files without an output directory");
return error.MultipleOutputFilesWithoutOutputDir;
}
Expand Down
3 changes: 3 additions & 0 deletions src/cli/build_command.zig
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ pub const BuildCommand = struct {
}

var outfile = ctx.bundler_options.outfile;
const output_to_stdout = !ctx.bundler_options.compile and outfile.len == 0 and ctx.bundler_options.outdir.len == 0;

this_bundler.options.supports_multiple_outputs = !(output_to_stdout or outfile.len > 0);

this_bundler.options.public_path = ctx.bundler_options.public_path;
this_bundler.options.entry_naming = ctx.bundler_options.entry_naming;
Expand Down
2 changes: 2 additions & 0 deletions src/options.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1529,6 +1529,8 @@ pub const BundleOptions = struct {
/// So we have a list of packages which we know are safe to do this with.
unwrap_commonjs_packages: []const string = &default_unwrap_commonjs_packages,

supports_multiple_outputs: bool = true,

pub fn isTest(this: *const BundleOptions) bool {
return this.rewrite_jest_for_tests;
}
Expand Down
43 changes: 43 additions & 0 deletions test/bundler/bundler_edgecase.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2031,6 +2031,49 @@ describe("bundler", () => {
},
});

itBundled("edgecase/NoOutWithTwoFiles", {
files: {
"/entry.ts": `
import index from './index.html'
console.log(index);
`,
"/index.html": `
<head></head>
`,
},
generateOutput: false,
backend: "api",
onAfterApiBundle: async build => {
expect(build.success).toEqual(true);
expect(build.outputs).toBeArrayOfSize(2);

expect(build.outputs[0].path).toEqual("./entry.js");
expect(build.outputs[0].loader).toEqual("ts");
expect(build.outputs[0].kind).toEqual("entry-point");

expect(build.outputs[1].loader).toEqual("file");
expect(build.outputs[1].kind).toEqual("asset");
expect(await build.outputs[1].text()).toEqual("<head></head>");
},
});

itBundled("edgecase/OutWithTwoFiles", {
files: {
"/entry.ts": `
import index from './index.html'
console.log(index);
`,
"/index.html": `
<head></head>
`,
},
generateOutput: true,
bundleErrors: {
"<bun>": ["cannot write multiple output files without an output directory"],
},
run: true,
});

// TODO(@paperdave): test every case of this. I had already tested it manually, but it may break later
const requireTranspilationListESM = [
// input, output:bun, output:node
Expand Down
20 changes: 15 additions & 5 deletions test/bundler/expectBundled.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* See `./expectBundled.md` for how this works.
*/
import { BuildConfig, BunPlugin, fileURLToPath, PluginBuilder } from "bun";
import { BuildConfig, BuildOutput, BunPlugin, fileURLToPath, PluginBuilder } from "bun";
import { callerSourceOrigin } from "bun:jsc";
import type { Matchers } from "bun:test";
import * as esbuild from "esbuild";
Expand Down Expand Up @@ -277,6 +277,12 @@ export interface BundlerTestInput {
timeoutScale?: number;
/** Multiplier for test timeout when using bun-debug. Debug builds already have a higher timeout. */
debugTimeoutScale?: number;

/* determines whether or not anything should be passed to outfile, outdir, etc. */
generateOutput?: boolean;

/** Run after the bun.build function is called with its output */
onAfterApiBundle?(build: BuildOutput): Promise<void> | void;
}

export interface SourceMapTests {
Expand Down Expand Up @@ -461,6 +467,8 @@ function expectBundled(
// @ts-expect-error
_referenceFn,
expectExactFilesize,
generateOutput = true,
onAfterApiBundle,
...unknownProps
} = opts;

Expand Down Expand Up @@ -494,7 +502,7 @@ function expectBundled(
if (metafile === true) metafile = "/metafile.json";
if (bundleErrors === true) bundleErrors = {};
if (bundleWarnings === true) bundleWarnings = {};
const useOutFile = outfile ? true : outdir ? false : entryPoints.length === 1;
const useOutFile = generateOutput == false ? false : outfile ? true : outdir ? false : entryPoints.length === 1;

if (bundling === false && entryPoints.length > 1) {
throw new Error("bundling:false only supports a single entry point");
Expand Down Expand Up @@ -555,8 +563,10 @@ function expectBundled(
external = external.map(x => (typeof x !== "string" ? x : x.replace(/\{\{root\}\}/g, root)));
}

if (generateOutput === false) outputPaths = [];

outfile = useOutFile ? path.join(root, outfile ?? (compile ? "/out" : "/out.js")) : undefined;
outdir = !useOutFile ? path.join(root, outdir ?? "/out") : undefined;
outdir = !useOutFile && generateOutput ? path.join(root, outdir ?? "/out") : undefined;
metafile = metafile ? path.join(root, metafile) : undefined;
outputPaths = (
outputPaths
Expand Down Expand Up @@ -984,7 +994,7 @@ function expectBundled(
},
plugins: pluginArray,
treeShaking,
outdir: buildOutDir,
outdir: generateOutput ? buildOutDir : undefined,
sourcemap: sourceMap,
splitting,
target,
Expand Down Expand Up @@ -1024,11 +1034,11 @@ for (const [key, blob] of build.outputs) {

configRef = buildConfig;
const build = await Bun.build(buildConfig);
if (onAfterApiBundle) await onAfterApiBundle(build);
configRef = null!;
Bun.gc(true);

const buildLogs = build.logs.filter(x => x.level === "error");

if (buildLogs.length) {
const allErrors: ErrorMeta[] = [];
for (const error of buildLogs) {
Expand Down

0 comments on commit 9e317c7

Please sign in to comment.