Include a module everywhere using "-open Helper" in bsc-flags fail when compiling Helper.res

Hello, I want to include a a project module (Helper.res) in all my project modules. I use this in bsconfig.js

"bsc-flags": [
  "-open Helper"
]

It works fine, but when I modify the Helper.res file, I get an error, because it try to include itself…
A workaround is removing the flag in bsconfig.js, compile, and put it again, whenever the file is modified.

rescript: [38/60] src/Helper.cmj
FAILED: src/Helper.cmj

  We've found a bug for you!
  command line

  The module or file Helper can't be found.
  - If it's a third-party dependency:
    - Did you list it in bsconfig.json?
    - Did you run `rescript build` instead of `rescript build -with-deps`
      (latter builds third-parties)?
  - Did you include the file's directory in bsconfig.json?

FAILED: cannot make progress due to previous errors.

Is there a better way ?

Unfortunately, you would need to use npm workspaces (or similar) for it, because -open only really works for separate packages. This means you then need to have three bsconfig.json files and three package.json files.

.
β”œβ”€β”€ bsconfig.json
β”œβ”€β”€ helpers
β”‚   β”œβ”€β”€ bsconfig.json
β”‚   β”œβ”€β”€ package.json
β”‚   └── src
β”‚       └── Helpers.res
β”œβ”€β”€ myproject
β”‚   β”œβ”€β”€ bsconfig.json
β”‚   β”œβ”€β”€ package.json
β”‚   └── src
β”‚       └── Demo.res
└── package.json

Here is what they need to look like (click to open):

./bsconfig.json
{
  ...
  "bs-dependencies": [
    "myproject",
    "helpers"
  ],
  "pinned-dependencies": [
    "myproject",
    "helpers"
  ]
}
./package.json
{
  ...
  "workspaces": {
    "packages": [
      "myproject",
      "helpers"
    ]
  }
}
helpers/bsconfig.json
{
  "name": "helpers",
  "sources": {
    "dir": "src",
    "subdirs": true
  }
}
helpers/package.json
{
  "name": "helpers",
  "version": "0.0.0",
  "private": true
}
myproject/bsconfig.json
{
  "name": "myproject",
  "sources": {
    "dir": "src",
    "subdirs": true
  },
  "bs-dependencies": [
    "helpers"
  ],
  "bsc-flags": [
    "-open Helpers"
  ]
}
myproject/package.json
{
  "name": "myproject",
  "version": "0.0.0",
  "private": true
}

Furthermore, for ReScript monorepo setups it is recommended to not use the internal watcher (see Pinned Dependencies | ReScript Language Manual), but something like watchexec. You can add it as a top-level package.json script like so:

...
  "scripts": {
    ...
    "watch": "watchexec --project-origin . -c -w myproject/src -w helpers/src -i lib -e res,resi 'rescript build -with-deps'"
  },
4 Likes