Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix relative project root #7285

Merged
merged 9 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 128 additions & 0 deletions packages/wrangler/src/__tests__/get-entry.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import { writeFile } from "fs/promises";

Check failure on line 1 in packages/wrangler/src/__tests__/get-entry.test.ts

View workflow job for this annotation

GitHub Actions / Checks

'writeFile' is defined but never used. Allowed unused vars must match /^_/u

Check failure on line 1 in packages/wrangler/src/__tests__/get-entry.test.ts

View workflow job for this annotation

GitHub Actions / Checks

'writeFile' is defined but never used
import path from "path";

Check failure on line 2 in packages/wrangler/src/__tests__/get-entry.test.ts

View workflow job for this annotation

GitHub Actions / Checks

'path' is defined but never used. Allowed unused vars must match /^_/u

Check failure on line 2 in packages/wrangler/src/__tests__/get-entry.test.ts

View workflow job for this annotation

GitHub Actions / Checks

'path' is defined but never used
import dedent from "ts-dedent";
import { Config } from "../config";

Check failure on line 4 in packages/wrangler/src/__tests__/get-entry.test.ts

View workflow job for this annotation

GitHub Actions / Checks

'Config' is defined but never used. Allowed unused vars must match /^_/u

Check failure on line 4 in packages/wrangler/src/__tests__/get-entry.test.ts

View workflow job for this annotation

GitHub Actions / Checks

'Config' is defined but never used
import { defaultWranglerConfig } from "../config/config";
import { Entry, getEntry } from "../deployment-bundle/entry";

Check failure on line 6 in packages/wrangler/src/__tests__/get-entry.test.ts

View workflow job for this annotation

GitHub Actions / Checks

Import "Entry" is only used as types
import guessWorkerFormat from "../deployment-bundle/guess-worker-format";

Check failure on line 7 in packages/wrangler/src/__tests__/get-entry.test.ts

View workflow job for this annotation

GitHub Actions / Checks

'guessWorkerFormat' is defined but never used. Allowed unused vars must match /^_/u

Check failure on line 7 in packages/wrangler/src/__tests__/get-entry.test.ts

View workflow job for this annotation

GitHub Actions / Checks

'guessWorkerFormat' is defined but never used
import { mockConsoleMethods } from "./helpers/mock-console";
import { runInTempDir } from "./helpers/run-in-tmp";
import { seed } from "./helpers/seed";

function normalize(entry: Entry): Entry {
return JSON.parse(
JSON.stringify(entry).replaceAll(process.cwd(), "/tmp/dir")
);
}
describe("getEntry()", () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙏 🙇 ♥️

runInTempDir();
mockConsoleMethods();
it.each([
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love that you added these tests, but if I am honest, using it.each() here actually obscures what the tests are doing and doesn't save much boilerplate compared to writing out a bunch of separate it clauses, given how simple the test code is.

[
"--script index.ts",
{
"index.ts": dedent/* javascript */ `
export default {
fetch() {

}
}
`,
},
{ script: "index.ts" },
{},
{
directory: "/tmp/dir",
file: "/tmp/dir/index.ts",
moduleRoot: "/tmp/dir",
},
],
[
"--script src/index.ts",
{
"src/index.ts": dedent/* javascript */ `
export default {
fetch() {

}
}
`,
},
{ script: "src/index.ts" },
{},
{
directory: "/tmp/dir",
file: "/tmp/dir/src/index.ts",
moduleRoot: "/tmp/dir/src",
},
],
[
"main = index.ts",
{
"index.ts": dedent/* javascript */ `
export default {
fetch() {

}
}
`,
},
{},
{ main: "index.ts" },
{
directory: "/tmp/dir",
file: "/tmp/dir/index.ts",
moduleRoot: "/tmp/dir",
},
],
[
"main = src/index.ts",
{
"src/index.ts": dedent/* javascript */ `
export default {
fetch() {

}
}
`,
},
{},
{ main: "src/index.ts" },
{
directory: "/tmp/dir",
file: "/tmp/dir/src/index.ts",
moduleRoot: "/tmp/dir/src",
},
],
[
"main = src/index.ts w/ configPath",
{
"other-worker/src/index.ts": dedent/* javascript */ `
export default {
fetch() {

}
}
`,
},
{},
{
main: "src/index.ts",
configPath: "other-worker/wrangler.toml",
},
{
directory: "/tmp/dir/other-worker",
file: "/tmp/dir/other-worker/src/index.ts",
moduleRoot: "/tmp/dir/other-worker/src",
},
],
])("%s", async (_name, files, args, config, result) => {
await seed(files);
const entry = await getEntry(
args,
{ ...defaultWranglerConfig, ...config },
"deploy"
);
expect(normalize(entry)).toMatchObject(result);
});
});
9 changes: 6 additions & 3 deletions packages/wrangler/src/deployment-bundle/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ export async function getEntry(
const directory = process.cwd();
const entryPoint = config.site?.["entry-point"];

let paths: { absolutePath: string; relativePath: string } | undefined;
let paths:
| { absolutePath: string; relativePath: string; directory?: string }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
| { absolutePath: string; relativePath: string; directory?: string }
| { absolutePath: string; relativePath: string; projectRoot?: string }

| undefined;

if (args.script) {
paths = resolveEntryWithScript(args.script);
Expand Down Expand Up @@ -81,9 +83,10 @@ export async function getEntry(
}
await runCustomBuild(paths.absolutePath, paths.relativePath, config.build);

const projectRoot = paths.directory ?? directory;
const { format, exports } = await guessWorkerFormat(
paths.absolutePath,
directory,
projectRoot,
args.format ?? config.build?.upload?.format,
config.tsconfig
);
Expand Down Expand Up @@ -117,7 +120,7 @@ export async function getEntry(

return {
file: paths.absolutePath,
directory,
directory: projectRoot,
format,
moduleRoot:
args.moduleRoot ?? config.base_dir ?? path.dirname(paths.absolutePath),
Expand Down
6 changes: 4 additions & 2 deletions packages/wrangler/src/deployment-bundle/resolve-entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ export function resolveEntryWithMain(
): {
absolutePath: string;
relativePath: string;
directory: string;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we call this projectRoot? Will simplify code elsewhere and is more descriptive than directory which could be a directory of anything.

} {
const directory = path.resolve(path.dirname(configPath ?? "."));
const file = path.resolve(directory, main);
const relativePath = path.relative(directory, file) || ".";
return { absolutePath: file, relativePath };
return { absolutePath: file, relativePath, directory };
}

export function resolveEntryWithEntryPoint(
Expand All @@ -29,13 +30,14 @@ export function resolveEntryWithEntryPoint(
): {
absolutePath: string;
relativePath: string;
directory: string;
} {
const directory = path.resolve(path.dirname(configPath ?? "."));
const file = path.extname(entryPoint)
? path.resolve(entryPoint)
: path.resolve(entryPoint, "index.js");
const relativePath = path.relative(directory, file) || ".";
return { absolutePath: file, relativePath };
return { absolutePath: file, relativePath, directory };
}

export function resolveEntryWithAssets(): {
Expand Down
Loading