beemojs / beemo

🤖 Centralized configuration layer for dev tools. Beep boop.
https://beemo.dev
MIT License
143 stars 9 forks source link

Script.tool is undefined #146

Closed niieani closed 3 years ago

niieani commented 3 years ago

As the title states, Script.tool is undefined.

Reproduction:

yarn add @niieani/beemo-build-tools@0.0.8 --dev

Add a .config/beemo.ts:

import {BeemoConfig} from '@beemo/core'

const config: BeemoConfig = {
  module: '@niieani/beemo-build-tools',
  drivers: ['babel', 'eslint', 'jest', 'prettier', 'typescript', 'webpack'],
}

export default config

Run scaffolding script:

yarn beemo run-script init-project

It will crash, because of script.tool being undefined here:

    const packageJson = context.script.tool.project.getPackage()

The line has a non-null assertion when it's defined: https://github.com/beemojs/beemo/blob/148035874cd7572aa7c04105b274d452b2e86655/packages/core/src/Script.ts#L14-L15

I've tried going through all the hoops of where startup is being run to understand why it's not being set, but I can't find the cause. Must be somewhere deep in @boost/plugin. When I attach a debugger in startup, it already receives undefined under the tool parameter during startup:

https://github.com/beemojs/beemo/blob/148035874cd7572aa7c04105b274d452b2e86655/packages/core/src/Script.ts#L41-L44

Workaround is to add a bootstrap function which sets the missing tool:

export default async function bootstrap(tool: Tool) {
  tool.scriptRegistry.onAfterRegister.listen((script) => {
    script.tool = tool
  })
}

Interestingly, the onAfterRegister is being called twice for the same script, not sure why? 🤷

milesj commented 3 years ago

The tool is set here: https://github.com/beemojs/beemo/blob/master/packages/core/src/Tool.ts#L133

Do you have a stack trace? I've never run into this before.

niieani commented 3 years ago

My config doesn't contain any scripts listed, which means that line you cited is run, but doesn't load any scripts. However the scripts don't have to be listed to work, because they're being loaded by RunScriptRoutine anyway. I've done a little more debugging and it looks like that's where the issue was, tool wasn't being passed in there.

Made a PR #149.