{"record":{"id":"8d78aaca1c8b0ff4","repo":"pulumi/pulumi","slug":"getting-current-working-directory-w-8d78aa","errorCode":null,"errorMessage":"getting current working directory: %w","messagePattern":"getting current working directory: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/cmd/pulumi/cloud/resolve.go","lineNumber":70,"sourceCode":"\tStackProj string\n\tStackName string\n\tLoggedIn  bool\n}\n\n// ResolveContext returns the Pulumi Cloud context for a `pulumi api`\n// invocation. The resolved OrgName comes from pkgBackend.GetDefaultOrg\n// (which prefers a locally-configured default and falls back to the\n// backend's opinion) so {orgName} template vars resolve sensibly even\n// outside a project directory.\n//\n// Credential lookup is non-interactive: when no credentials are stored,\n// ResolveContext returns an anonymous Client + the resolved CloudURL with\n// LoggedIn=false rather than prompting or failing. Callers that require\n// authentication should check LoggedIn and surface their own error.\nfunc ResolveContext(ctx context.Context) (*ResolvedContext, error) {\n\tcwd, err := os.Getwd()\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"getting current working directory: %w\", err)\n\t}\n\n\tws := pkgWorkspace.Instance\n\tproject, _, err := ws.ReadProject(cwd)\n\tif err != nil && !errors.Is(err, workspace.ErrProjectNotFound) {\n\t\treturn nil, fmt.Errorf(\"reading project: %w\", err)\n\t}\n\n\t// Resolve the URL ourselves before probing credentials so we honour\n\t// a project-declared backend (Pulumi.yaml's `backend.url`) without\n\t// triggering CurrentBackend's interactive login path.\n\tvar projectURL string\n\tif project != nil {\n\t\tif u, perr := pkgWorkspace.GetCurrentCloudURL(ws, env.Global(), project); perr == nil {\n\t\t\tprojectURL = u\n\t\t}\n\t}\n\tcloudURL := httpstate.ValueOrDefaultURL(ws, projectURL)","sourceCodeStart":52,"sourceCodeEnd":88,"githubUrl":"https://github.com/pulumi/pulumi/blob/793f7b2e160db4321fb7fb6b0607461e01cb251e/pkg/cmd/pulumi/cloud/resolve.go#L52-L88","documentation":"ResolveContext resolves the Pulumi Cloud context for `pulumi api` commands, starting by reading the current working directory via os.Getwd. If the OS cannot determine the working directory (e.g. it was deleted or is unreadable), the error is wrapped as 'getting current working directory' and resolution aborts before any project or credential lookup.","triggerScenarios":"os.Getwd fails — typically because the process's current directory no longer exists (deleted while the shell is inside it), the inode is stale after a mount/removal, or a permissions/environment issue prevents path resolution.","commonSituations":"Running `pulumi api` in a shell whose cwd was deleted (e.g. `rm -rf` of the directory in another terminal, or a CI workspace cleaned up mid-run); running inside a container with a removed workdir; running from a directory on an unmounted volume.","solutions":["cd to a directory that exists (e.g. cd ~ or cd to your project) and re-run the command","If in a deleted-directory shell, open a new shell or `cd $(pwd)` after the directory is recreated","In scripts/CI, ensure the working directory exists before invoking pulumi commands"],"exampleFix":"// before (shell in a deleted dir)\npulumi api list\n\n// after\nprojectdir=/path/to/project\ncd \"$projectdir\" || exit 1\npulumi api list","handlingStrategy":"validation","validationCode":"if wd, err := os.Getwd(); err != nil {\n\tfmt.Fprintf(os.Stderr, \"working directory unavailable: %v\\n\", err)\n\tos.Exit(1)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Run pulumi commands from an existing, mounted directory","In CI, verify the workspace directory exists before steps that invoke pulumi","After deleting a project directory, cd elsewhere before running CLI commands"],"tags":["filesystem","working-directory","cli"],"backgroundTag":"working-directory-unavailable","analyzedSha":"793f7b2e160db4321fb7fb6b0607461e01cb251e","analyzedAt":"2026-08-31T09:36:43.099Z","schemaVersion":2},"datasetVersion":"2026-09-01T08:17:40.651Z"}