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

Preserve order of execution #128

Open
thelinuxlich opened this issue Feb 21, 2025 · 5 comments
Open

Preserve order of execution #128

thelinuxlich opened this issue Feb 21, 2025 · 5 comments

Comments

@thelinuxlich
Copy link

I have a task triggering pnpm install whenever pnpm-lock.yaml changes and it should run before other tasks that might be dependent on it.

@hkdobrev
Copy link
Owner

hkdobrev commented Feb 21, 2025

@thelinuxlich can you provide your config and how do you imagine this working in terms of configuration API?

@thelinuxlich
Copy link
Author

Sure, I have something like this in my package.json:

"run-if-changed": {
    "pnpm-lock.yaml": "pnpm i",
    "prisma/rise/migrations/**/*.sql": "pnpm migrate-all",
    "prisma/rise_private/migrations/**/*.sql": "pnpm migrate-all",
    "prisma/rise_audit/migrations/**/*.sql": "pnpm migrate-all",
    "packages/db/src/seed.ts": "pnpm --filter db seed && pnpm codegen",
    "packages/db/src/helpers/contracts.ts": "pnpm --filter db seed && pnpm codegen",
    "packages/db/src/helpers/*.json": "pnpm --filter db seed && pnpm codegen"
  }

I'd expect nothing changes config-wise, just that run-if-changed respects the order defined here. (I need packages installed and migrations applied before running codegen)

@hkdobrev
Copy link
Owner

It might work if we reverse the order in which run-if-changed processes the state.
Currently, it asks Git for the changed files and tries to find matching patterns for each of them in the config.
If we instead read the config of patterns first and try to find matching files in the changed files from Git, it might work the way you describe.

Would try to give it a go in the following days.

In the meantime, you can repeat some commands so that it also installs packages and runs migration before running codegen.

@hkdobrev
Copy link
Owner

Hm, actually run-if-changed already does that in the order I described, but in the process of flattening the commands the order can get scrambled:

export default (changedFiles, config) => {
const commandsToRun = Object.entries(config)
.filter(([pattern]) => micromatch.some(changedFiles, [pattern]))
.flatMap(([, commands]) => commands);
// unique commands, do not run them multiple times if they are the same
// for multiple patterns or they are repeated in a list for a single pattern
return [...new Set(commandsToRun)];
};

Might need to play with that further, but I'm afraid it will be hard to make it work reliably without changing the config structure.

@hkdobrev
Copy link
Owner

@thelinuxlich OK, I tried this something very close to your config.

It does work like you would expect. It goes through the configured patterns in order. The alphabetic order doesn't matter, but the order you used in your configuration file.

If files found in multiple patterns are changed, then it will first run the commands attached to the earlier patterns in the config.

If you are not seeing that, could you show a reproducible demo?

Please check that multiple patterns are affected at the same time. It might be that only later commands are run when the lock file hasn't changed. If you want the installation or migration commands to run before codegen even when the respective files are unchanged, you would need to repeat these commands before the codegen command.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants