pulumi/sdk/nodejs/metadata.ts
Joe Duffy 28033c22bc
Allow multiple Pulumi SDKs side-by-side (#1132)
Prior to this change, if you ended up with multiple Pulumi SDK
packages loaded side-by-side, we would fail in obscure ways.  The
reason for this was that we initialize and store important state
in static variables.  In the case that you load the same library
twice, however, you end up with separate copies of said statics,
which means we would be missing engine RPC addresses and so on.

This change adds the ability to recover from this situation by
mirroring the initialized state in process-wide environment
variables.  By doing this, we can safely recover simply by reading
them back when we detect that they are missing.  I think we can
eventually go even further here, and eliminate the entry point
launcher shim altogether by simply having the engine launch the
Node program with the right environment variables.  This would
be a nice simplification to the system (fewer moving pieces).

There is still a risk that the separate copy is incompatible.
Presumably the reason for loading multiple copies is that the
NPM/Yarn version solver couldn't resolve to a shared version.
This may yield obscure failure modes should RPC interfaces change.
Figuring out what to do here is part of pulumi/pulumi#957.

This fixes pulumi/pulumi#777 and pulumi/pulumi#1017.
2018-04-07 08:02:59 -07:00

27 lines
784 B
TypeScript

// Copyright 2016-2018, Pulumi Corporation. All rights reserved.
// This file exports metadata about the context in which a program is being run.
import * as runtime from "./runtime";
/**
* getProject returns the current project name. It throws an exception if none is registered.
*/
export function getProject(): string {
const project = runtime.getProject();
if (project) {
return project;
}
throw new Error("Project unknown; are you using the Pulumi CLI?");
}
/**
* getStack returns the current stack name. It throws an exception if none is registered.
*/
export function getStack(): string {
const stack = runtime.getStack();
if (stack) {
return stack;
}
throw new Error("Stack unknown; are you using the Pulumi CLI?");
}