mirror of
https://github.com/windmill-labs/windmill.git
synced 2026-12-01 08:10:13 +00:00
fix(cli): keep generate-metadata inside the named folder (#12668)
* fix(cli): keep generate-metadata inside the named folder when creating metadata Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(cli): match file targets and dependency folders in the empty-folder check Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(cli): base the includes hint on include/exclude rules only Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
462c16bb12
commit
2c2273cffb
@@ -1,7 +1,8 @@
|
||||
import { Command } from "@cliffy/command";
|
||||
import { Confirm } from "@cliffy/prompt/confirm";
|
||||
import { colors } from "@cliffy/ansi/colors";
|
||||
import { sep as SEP } from "node:path";
|
||||
import { readdir, stat } from "node:fs/promises";
|
||||
import { join, sep as SEP } from "node:path";
|
||||
import { GlobalOptions, isDatatableMigrationPath } from "../../types.ts";
|
||||
import { isFileResource, isFilesetResource } from "../../utils/utils.ts";
|
||||
import { SyncOptions, mergeConfigWithConfigFile } from "../../core/conf.ts";
|
||||
@@ -412,6 +413,29 @@ export async function rehashOnly(
|
||||
return counts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether `target` (a file, or any file under a folder) is dropped by the
|
||||
* includes/excludes alone: `ignore` also drops every file that is not a
|
||||
* Windmill item, which `unfiltered` (built without those rules) drops too.
|
||||
*/
|
||||
async function isFilteredByIncludes(
|
||||
target: string,
|
||||
ignore: (p: string, isD: boolean) => boolean,
|
||||
unfiltered: (p: string, isD: boolean) => boolean,
|
||||
): Promise<boolean> {
|
||||
const native = target.replaceAll("/", SEP);
|
||||
const filtered = (p: string) => ignore(p, false) && !unfiltered(p, false);
|
||||
let info;
|
||||
try {
|
||||
info = await stat(native);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
if (!info.isDirectory()) return filtered(native);
|
||||
const entries = await readdir(native, { recursive: true, withFileTypes: true });
|
||||
return entries.some((e) => e.isFile() && filtered(join(e.parentPath, e.name)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize the tree's shared lockfiles, unless the run asked to stay inside one
|
||||
* folder: shared lockfiles are workspace-wide, so the pass reads and rewrites
|
||||
@@ -473,6 +497,18 @@ export async function generateMetadata(
|
||||
if (folder === "") {
|
||||
folder = undefined;
|
||||
}
|
||||
// Normalize to forward slashes (Windows users may use backslashes) and strip
|
||||
// a trailing slash to match deprecated flow/app handler behavior
|
||||
folder = folder?.replaceAll("\\", "/").replace(/\/$/, "");
|
||||
// Strip file extension if user passed a specific file path (e.g. f/test/script.ts)
|
||||
const folderNoExt = folder?.replace(/\.[^/.]+$/, "");
|
||||
const isPathInFolder = (p: string) =>
|
||||
folder !== undefined &&
|
||||
(p.startsWith(folder + "/") || p === folder || p === folderNoExt);
|
||||
let walkedInFolder = false;
|
||||
const noteWalked = (p: string) => {
|
||||
if (isPathInFolder(p.replaceAll("\\", "/"))) walkedInFolder = true;
|
||||
};
|
||||
|
||||
const workspace = await resolveWorkspace(opts);
|
||||
await requireLogin(opts);
|
||||
@@ -506,6 +542,10 @@ export async function generateMetadata(
|
||||
// === Collect stale scripts ===
|
||||
if (!skipScripts) {
|
||||
for (const e of await walkLocalScripts(codebases, ignore)) {
|
||||
// A file target (`f/x/task.deno.ts`, `f/x/task__mod/script.ts`) matches the
|
||||
// local path, a folder target the remote one.
|
||||
noteWalked(e);
|
||||
noteWalked(scriptPathToRemotePath(e));
|
||||
await generateScriptMetadataInternal(
|
||||
e,
|
||||
workspace,
|
||||
@@ -523,6 +563,7 @@ export async function generateMetadata(
|
||||
// === Collect stale flows ===
|
||||
if (!skipFlows) {
|
||||
for (const flowFolder of await walkLocalFlowFolders(ignore)) {
|
||||
noteWalked(flowFolder);
|
||||
await generateFlowLockInternal(
|
||||
flowFolder,
|
||||
true, // dryRun - populate tree
|
||||
@@ -538,6 +579,7 @@ export async function generateMetadata(
|
||||
// === Collect stale apps ===
|
||||
if (!skipApps) {
|
||||
for (const { folder: appFolder, rawApp } of await walkLocalAppItems(ignore)) {
|
||||
noteWalked(appFolder);
|
||||
await generateAppLocksInternal(
|
||||
appFolder,
|
||||
rawApp,
|
||||
@@ -551,6 +593,27 @@ export async function generateMetadata(
|
||||
}
|
||||
}
|
||||
|
||||
// Tree nodes also cover workspace dependency files, which the walk never lists.
|
||||
if (folder && !walkedInFolder && ![...tree.allPaths()].some(isPathInFolder)) {
|
||||
const unfiltered = await ignoreF({
|
||||
...opts, includes: undefined, excludes: undefined, extraIncludes: undefined,
|
||||
});
|
||||
if (await isFilteredByIncludes(folder, ignore, unfiltered)) {
|
||||
const pattern = (await stat(folder.replaceAll("/", SEP))).isDirectory()
|
||||
? `${folder}/**`
|
||||
: folder;
|
||||
log.warn(colors.yellow(
|
||||
`Nothing under "${folder}" was checked: the includes/excludes in effect (wmill.yaml or -i/-e) filter it out. ` +
|
||||
`Pass --includes "${pattern}" to check it, after removing any excludes that match it.`
|
||||
));
|
||||
} else {
|
||||
log.warn(colors.yellow(`No scripts, flows or apps to check under "${folder}".`));
|
||||
}
|
||||
// Same reason as the up-to-date path below: the conversion still applies.
|
||||
await maybeDedupeLockfiles(opts, workspace, codebases, ignore, rawWorkspaceDependencies, tree, folder);
|
||||
return;
|
||||
}
|
||||
|
||||
// === Propagate staleness through imports ===
|
||||
tree.propagateStaleness();
|
||||
|
||||
@@ -597,14 +660,6 @@ export async function generateMetadata(
|
||||
// === Filter by folder if specified ===
|
||||
let filteredItems = staleItems;
|
||||
if (folder) {
|
||||
// Normalize to forward slashes (Windows users may use backslashes)
|
||||
folder = folder.replaceAll("\\", "/");
|
||||
// Strip trailing slash to match deprecated flow/app handler behavior
|
||||
if (folder.endsWith("/")) {
|
||||
folder = folder.substring(0, folder.length - 1);
|
||||
}
|
||||
// Strip file extension if user passed a specific file path (e.g. f/test/script.ts)
|
||||
const folderNoExt = folder.replace(/\.[^/.]+$/, "");
|
||||
// Check if an item is inside the specified folder
|
||||
const isInsideFolder = (item: StaleItem) => {
|
||||
const normalizedFolder = item.folder.replaceAll("\\", "/");
|
||||
@@ -612,7 +667,6 @@ export async function generateMetadata(
|
||||
return normalizedFolder === folder || normalizedFolder.startsWith(folder + "/")
|
||||
|| normalizedPath === folder || normalizedPath === folderNoExt;
|
||||
};
|
||||
const isPathInFolder = (p: string) => p.startsWith(folder + "/") || p === folder || p === folderNoExt;
|
||||
// Check if a tree path or any of its transitive deps is inside the folder
|
||||
const touchesFolder = (treePath: string) => {
|
||||
if (isPathInFolder(treePath)) return true;
|
||||
|
||||
+39
-22
@@ -229,12 +229,15 @@ export async function generateScriptMetadataInternal(
|
||||
// the metadata sync actually reads untouched.
|
||||
const metadataInFolder = isFolderLayout && language !== "dbt";
|
||||
|
||||
// For folder layout, parseMetadataFile is called with remotePath which
|
||||
// will find __mod/script.yaml via the folder layout fallback
|
||||
const metadataWithType = await parseMetadataFile(
|
||||
remotePath,
|
||||
undefined,
|
||||
);
|
||||
// A tree run's dry pass only detects staleness; it must not create missing
|
||||
// metadata, or a folder-scoped run writes files for every script it walks.
|
||||
// A script without metadata is stale, and the generation pass creates it.
|
||||
const detectOnly = tree !== undefined && dryRun && !opts.rehashOnly;
|
||||
// For folder layout, the lookup uses remotePath, which finds
|
||||
// __mod/script.yaml via the folder layout fallback
|
||||
const existingMetadata = detectOnly
|
||||
? await findMetadataFile(remotePath)
|
||||
: await parseMetadataFile(remotePath, undefined);
|
||||
|
||||
// read script content — a dbt project's descriptor is optional, and absent
|
||||
// means an empty descriptor rather than a script that cannot be pushed.
|
||||
@@ -242,7 +245,9 @@ export async function generateScriptMetadataInternal(
|
||||
if (isMissingDbtDescriptor(scriptPath, e)) return "";
|
||||
throw e;
|
||||
});
|
||||
const metadataContent = await readTextFile(metadataWithType.path);
|
||||
const metadataContent = existingMetadata
|
||||
? await readTextFile(existingMetadata.path)
|
||||
: "";
|
||||
|
||||
const filteredRawWorkspaceDependencies = filterWorkspaceDependencies(
|
||||
rawWorkspaceDependencies,
|
||||
@@ -306,7 +311,8 @@ export async function generateScriptMetadataInternal(
|
||||
}
|
||||
|
||||
// Use checkHash (includes module hashes) so module changes are detected as stale
|
||||
const isDirectlyStale = !(await checkifMetadataUptodate(remotePath, checkHash, conf, checkSubpath));
|
||||
const isDirectlyStale = !existingMetadata ||
|
||||
!(await checkifMetadataUptodate(remotePath, checkHash, conf, checkSubpath));
|
||||
|
||||
// Tree-based dependency tracking
|
||||
if (tree) {
|
||||
@@ -347,6 +353,9 @@ export async function generateScriptMetadataInternal(
|
||||
log.info(colors.gray(`Generating metadata for ${scriptPath}`));
|
||||
}
|
||||
|
||||
const metadataWithType = existingMetadata ??
|
||||
(await parseMetadataFile(remotePath, undefined));
|
||||
|
||||
const metadataParsedContent = metadataWithType?.payload as Record<
|
||||
string,
|
||||
any
|
||||
@@ -1088,18 +1097,10 @@ export async function parseMetadataFileIfExists(
|
||||
}
|
||||
}
|
||||
|
||||
export async function parseMetadataFile(
|
||||
scriptPath: string,
|
||||
generateMetadataIfMissing:
|
||||
| (GlobalOptions & {
|
||||
path: string;
|
||||
workspaceRemote: Workspace;
|
||||
schemaOnly?: boolean;
|
||||
rawWorkspaceDependencies: Record<string, string>;
|
||||
codebases: SyncCodebase[]
|
||||
})
|
||||
| undefined
|
||||
): Promise<{ isJson: boolean; payload: any; path: string }> {
|
||||
/** The script's metadata file, flat or folder layout, or undefined when it has none. */
|
||||
async function findMetadataFile(
|
||||
scriptPath: string
|
||||
): Promise<{ isJson: boolean; payload: any; path: string } | undefined> {
|
||||
let metadataFilePath = scriptPath + ".script.json";
|
||||
try {
|
||||
await stat(metadataFilePath);
|
||||
@@ -1143,13 +1144,29 @@ export async function parseMetadataFile(
|
||||
isJson: false,
|
||||
};
|
||||
} catch {
|
||||
// fall through to create metadata
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function parseMetadataFile(
|
||||
scriptPath: string,
|
||||
generateMetadataIfMissing:
|
||||
| (GlobalOptions & {
|
||||
path: string;
|
||||
workspaceRemote: Workspace;
|
||||
schemaOnly?: boolean;
|
||||
rawWorkspaceDependencies: Record<string, string>;
|
||||
codebases: SyncCodebase[]
|
||||
})
|
||||
| undefined
|
||||
): Promise<{ isJson: boolean; payload: any; path: string }> {
|
||||
const existing = await findMetadataFile(scriptPath);
|
||||
if (existing) return existing;
|
||||
// no metadata file at all. Create it
|
||||
metadataFilePath = scriptPath + ".script.yaml";
|
||||
const metadataFilePath = scriptPath + ".script.yaml";
|
||||
log.info(
|
||||
(await blueColor())(
|
||||
`Creating script metadata file for ${metadataFilePath}`
|
||||
|
||||
@@ -595,7 +595,7 @@ describe("generate-metadata folder argument", () => {
|
||||
});
|
||||
});
|
||||
|
||||
test("non-existent folder shows up-to-date", async () => {
|
||||
test("non-existent folder says nothing was found", async () => {
|
||||
await withTestBackend(async (backend, tempDir) => {
|
||||
await setupWorkspace(backend, tempDir, "nonexistent_folder_test");
|
||||
|
||||
@@ -609,7 +609,60 @@ describe("generate-metadata folder argument", () => {
|
||||
);
|
||||
|
||||
expect(result.code).toEqual(0);
|
||||
expect(result.stdout).toContain("up-to-date");
|
||||
const output = result.stdout + result.stderr;
|
||||
expect(output).toContain('No scripts, flows or apps to check under "f/does_not_exist"');
|
||||
expect(output).not.toContain("up-to-date");
|
||||
});
|
||||
});
|
||||
|
||||
test("--strict-folder-boundaries creates no metadata outside the folder", async () => {
|
||||
await withTestBackend(async (backend, tempDir) => {
|
||||
await setupWorkspace(backend, tempDir, "strict_missing_metadata_test");
|
||||
|
||||
await createLocalScript(tempDir, "f/panel", "edited");
|
||||
// Scripts kept without metadata on purpose, outside the folder
|
||||
await mkdir(`${tempDir}/f/starbase`, { recursive: true });
|
||||
await writeFile(
|
||||
`${tempDir}/f/starbase/bare.ts`,
|
||||
`export async function main() {\n return 1;\n}\n`,
|
||||
"utf-8"
|
||||
);
|
||||
|
||||
const result = await backend.runCLICommand(
|
||||
["generate-metadata", "--yes", "f/panel", "--strict-folder-boundaries"],
|
||||
tempDir,
|
||||
"strict_missing_metadata_test"
|
||||
);
|
||||
|
||||
expect(result.code).toEqual(0);
|
||||
expect(result.stdout + result.stderr).toContain("f/panel/edited");
|
||||
await expect(stat(`${tempDir}/f/starbase/bare.script.yaml`)).rejects.toThrow();
|
||||
await expect(stat(`${tempDir}/f/starbase/bare.script.lock`)).rejects.toThrow();
|
||||
const lockfile = await readFile(`${tempDir}/wmill-lock.yaml`, "utf-8");
|
||||
expect(lockfile).toContain("f/panel/edited");
|
||||
});
|
||||
});
|
||||
|
||||
test("folder outside the includes says it was not checked", async () => {
|
||||
await withTestBackend(async (backend, tempDir) => {
|
||||
await setupWorkspace(backend, tempDir, "folder_outside_includes_test");
|
||||
await writeFile(`${tempDir}/wmill.yaml`, `defaultTs: bun
|
||||
includes:
|
||||
- "f/starbase/**"
|
||||
excludes: []`, "utf-8");
|
||||
|
||||
await createLocalScript(tempDir, "f/panel", "edited");
|
||||
|
||||
const result = await backend.runCLICommand(
|
||||
["generate-metadata", "--yes", "f/panel"],
|
||||
tempDir,
|
||||
"folder_outside_includes_test"
|
||||
);
|
||||
|
||||
expect(result.code).toEqual(0);
|
||||
const output = result.stdout + result.stderr;
|
||||
expect(output).toContain('Nothing under "f/panel" was checked');
|
||||
expect(output).not.toContain("up-to-date");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user