Run a task that has a relative file path

I want to run a task in the root directory that uses a relative file reference in one of my packages. This is producing a not found error as the file is relative to the package, not to the root.

I have a db package that is using kysely and kysely-codegen to generate a schema file. I am using the following shell script placed in packages/db/bin/generate_schema.sh to do this

SCRIPT_DIR=$(cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd)
ROOT=$(realpath "$SCRIPT_DIR/..")

cat ${ROOT}/sql/create_db.sql | sqlite3 local.db;
DATABASE_URL=local.db bunx kysely-codegen --out-file ${ROOT}/src/schema/db.d.ts;
rm local.db;

In my package.json I have the following scripts

 "scripts": {
    "dev": "tsc --watch",
    "db:generate": "sh ./bin/generate_protos.sh"
  },

And in my turbo.json I have added

"tasks": {
    ...,
    "db:generate": {}
  }

bun run db:generate works when I am in packages/db, but running turbo run db:generate produces the error sh: ./bin/generate_protos.sh: No such file or directory. This fails whether I am in the root directory or in packages/db

Is there a way I can support this behaviour? I am fairly new to turborepo so maybe there is something I am missing?

Hi, @prchristie! Welcome to the Vercel Community :smile:

I believe that the issue stems from a mismatch between the working directory when running scripts directly in a package versus running them through Turborepo from the root of your monorepo.

When you execute turbo run db:generate from the root, Turborepo runs the script in the context of the root directory, not the package directory. This causes relative paths in your scripts to fail because they’re being resolved from the wrong location.

The solution involves modifying your scripts to use absolute paths or environment variables that correctly reference the required files and directories, regardless of where the script is initiated from.

Let us know how you get on!