Yarn build fails in deployment - [@roots/bud-preset-wordpress] › ✖ TypeError [ERR_INVALID_URL]: Invalid URL

Running into an issue running yarn build when deploying a new Sage 10 site, same command is working just fine in a local environment. Not sure what invalid URL the error in question here is referring to:

$ bud build
[?25l⠼
[?25l⠇
[@roots/bud-preset-wordpress] › ✖ TypeError [ERR_INVALID_URL]: Invalid URL
at new NodeError (node:internal/errors:372:5)
at URL.onParseError (node:internal/url:553:9)
at parse (<anonymous>)
at new URL (node:internal/url:629:5)
at Object.set origin [as origin] (file:///source/web/app/themes/custom-made-beanies/node_modules/@roots/bud-preset-wordpress/lib/extension.js:10:57)
at Object.init (file:///source/web/app/themes/custom-made-beanies/node_modules/@roots/bud-preset-wordpress/lib/extension.js:15:21)
at Object._init (file:///source/web/app/themes/custom-made-beanies/node_modules/@roots/bud-framework/lib/extension/index.js:63:24)
at async Extensions.run (file:///source/web/app/themes/custom-made-beanies/node_modules/@roots/bud-extensions/lib/service.js:214:13)
at async file:///source/web/app/themes/custom-made-beanies/node_modules/@roots/bud-extensions/lib/service.js:271:13
at async file:///source/web/app/themes/custom-made-beanies/node_modules/@roots/bud-extensions/lib/service.js:270:13
[bud@6.3.5] [bud] › ✖ error in @roots/bud-preset-wordpress

here’s my package contents:

"devDependencies": {
    "@popperjs/core": "^2.11.5",
    "@roots/bud": "6.3.5",
    "@roots/bud-sass": "^6.3.4",
    "@roots/sage": "6.3.5",
    "bootstrap": "^5.2.0"
  },
  "dependencies": {
    "glightbox": "^3.2.0",
    "gsap": "^3.10.3",
    "imagesloaded": "^5.0.0",
    "isotope-layout": "^3.0.6",
    "swiper": "^8.0.7",
    "vanilla-lazyload": "^17.8.1"
  }

Both yarn dev & yarn build are working locally, this seems to only be happening when building assets in deploybot for deployments.

What is the value of the WP_HOME envvar in the context of the deployment?

It is trying to do do this:

  @bind
  public async init() {
    if (!this.app.env.has('WP_HOME') || !this.app.env.isString('WP_HOME'))
      return
    this.origin = this.app.env.get('WP_HOME')
  }

In your local environment it must be a valid URL (if you have it set at all).

Ahh - I see. Because this is happening in CI there isn’t an env variable for that set yet. I’m working through how to get that going, but is there a way to set that variable from the command line before calling yarn build so this can compile?

the thing is that there must be an environment variable set in this context. if it didn’t exist it wouldn’t error.

if (!this.app.env.has('WP_HOME') || !this.app.env.isString('WP_HOME'))
      return

we know that it exists and is a string.

  1. you could just add the .env to your bud project directory:
# /app/themes/sage/.env
WP_HOME=http://example.com
  1. you could use cross-env or equivalent

Humm… unfortunately, those solutions won’t work as this project is being deployed to multiple environments, but those settings would be static.

From what I can tell, I guess WP_HOME is somehow defaulting to “/” (this is a Bedrock project if that helps) which is causing it to fail. Any chance that check inside of bud-preset-wordpress can be upgraded to to check for a URL rather than a string, which is actually needs if that var is present?

Phew… thanks for the help! Finally ended up tracking this down to some bad config for WP_HOME after all!