henke.fyi

Trying to try out the Pulumi Automation API

So i wanted to try out Pulumis Automation API to see what the potential for automating complex deployments, as well as for simplifying workflows that involve multuple stacks. This was what actually pushed me over the edge to create this website.

So, after taking a look at the getting started guide, i dove right in.

I creates a small library file with a super simple inline program

import * as aws from "@pulumi/aws";
import * as pulumi from "@pulumi/pulumi";

const args: pulumi.automation.InlineProgramArgs = {
  program: async () => {
    let bucket = new aws.s3.Bucket("automation-bucket");
    return { bucketArn: bucket.arn };
  },
  projectName: "dev",
  stackName: "automation-test",
};

export async function up() {
  const stack = await pulumi.automation.LocalWorkspace.createOrSelectStack(
    args
  );
  return { name: stack.name };
}

And an endpoint that called the up function

import type { RequestHandler } from "@sveltejs/kit";
import { up } from "../../../lib/pulumi/automation";

export const get: RequestHandler<{}, {}, { name: string }> = async (req) => {
  let body = await up();
  return { body };
};

Which I then called with curl

$ curl http://localhost:3000/experiments/pulumi/up
Error: Failed to resolve entry for package "spdx-license-ids". The package may have incorrect main/module/exports specified in its package.json: Failed to resolve entry for package "spdx-license-ids". The package may have incorrect main/module/exports specified in its package.json.
    at packageEntryFailure (/Users/henke/Development/experiments/node_modules/vite/src/node/plugins/resolve.ts:766:9)
    at resolvePackageEntry (/Users/henke/Development/experiments/node_modules/vite/src/node/plugins/resolve.ts:761:5)
    at tryNodeResolve (/Users/henke/Development/experiments/node_modules/vite/src/node/plugins/resolve.ts:539:16)
    at viteResolve (/Users/henke/Development/experiments/node_modules/vite/src/node/ssr/ssrModuleLoader.ts:218:22)
    at Function.<anonymous> (/Users/henke/Development/experiments/node_modules/vite/src/node/ssr/ssrModuleLoader.ts:238:16)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/Users/henke/Development/experiments/node_modules/spdx-expression-parse/scan.js:4:11)

It did not work.

A quick googling turned up that i was not the first person to hit this.

The error message says that the package.json file of the spdx-license-ids package might not be set up correctly. I did some digging and it turns out that the package does not have any js files, it's main entrypoint is a index.json file. I took a look at the package.json spec and it says that the default value for main is index.js. A quick local edit of the node_modules/spdx-license-ids/package.json to add main: "index.json" solves the issue, but it appears that the module spdx-exceptions has the same problem. Modifying spdx-exceptions in the same way gets us where we want to go, and now the endpoint returns the name of the stack!

$ curl http://localhost:3000/experiments/pulumi/up
{"name":"automation-test"}

So a fork and a quick commit later (thanks to the GitHub Repositories extension for VSCode I did not even have to manually clone the repo, just "open" it, make the change and commit.), I am ready to override the version of the library with my fork.

My Svelte project uses npm as the package manages, because that is what the template used. And it seems that just recently npm released support for the overrides configuration. "A piece of cake then" I thought and added some overrides to my package.json.

"overrides": {
  "spdx-license-ids": "git+https://github.com/henriiik/spdx-license-ids.git"
},

It did not work.

"dependencies": {
  /// ....
  "spdx-license-ids": "git+https://github.com/henriiik/spdx-license-ids.git"
},
"overrides": {
  "spdx-license-ids": "$spdx-license-ids"
},

Adding spdx-license-ids as a dependency, and refering to it from overrides did work however. It is unclear if this is the intended behvior.

I repeated the process for spdx-exceptions and now i am able to run the pulumi automation code to get or create a stack!

Which of course means it's time to start creating PRs to the upstream repos with my changes. Said and done, PRs were made to spdx-exceptions and spdx-license-ids.

But within 24h the first one was closed, with the reasoning that the node module resolution algorithm falls back to index.json if index.js when main is falsy. As is clearly stated by the node documentation, if you knew to look there.

So I thought maybe I had made a mistake with the configuration, and i took another look at the vite docs. But the only thing that looked relevant was the resolve.extensions configuration and includes .json by default.

A search in the repository for index.json showed that it only appears in the previously mentioned discussion. Taking a look at the stacktrace instead lead me to look at vite/src/node/plugins/resolve.ts, in which as far as i can tell, the only fallback is index.js and not index.json.

So I decided to file a bug in the vite project, since the fix did not seem obvious. The experience of submitting the bug was very nice, as they have a template that guides the process very well, and a handy link to vite.new that makes it very easy to create a minimal reproduction directly in the browser.

to be continued..