~Is there no way to get the current stack name and...
# package-authoring
g
Is there no way to get the current stack name and project name from provider code? We have a pulumi framework in typescript, in which we auto generate resource names based on stack and project name. I managed to automatically generate a package schema from our TS code and build a provider package so we can let application engineers stitch together components with yaml. The only problem is
getStack()
and
getProject()
return the literal strings
stack
and
project
since it looks like they are normally sourced from env vars which are not available to the provider's process
I was able to get this from the parent urn inside my provider's
construct
method, but not without being hacky and accessing a private/internal method. Still would like to figure out a better way to do this.
Copy code
async construct(
    name: string,
    type: string,
    inputs: pulumi.Inputs,
    options: pulumi.ComponentResourceOptions
  ): Promise<provider.ConstructResult> {
    const [, , stack, , program] = (
      await (<any>options.parent?.urn)["promise"]()
    ).split(":");

    // more code
  }
I see the issue now - the class responsible for the auto-naming was instantiated in module scope, so the project and stack name were initialized when the framework is imported. That works fine when running in the nodejs language runtime because
runtime.settings.LocalStore
sets stack/project using env var values as I mentioned above. If not set they default to literal strings
project
and
stack
. When running in context of a provider, the `Server`'s
construct
method configures the runtime based on the values in the
ConstructRequest
. tl;dr - instead of calling
getProject()
and
getStack()
to set the
name
value in my class from its constructor, I made
name
a getter and now it works as expected