{"record":{"id":"1ee4f973c4255887","repo":"hashicorp/terraform","slug":"can-not-get-working-directory-for-current-os-platf","errorCode":null,"errorMessage":"can not get working directory for current os platform","messagePattern":"can not get working directory for current os platform","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/backend/remote-state/oci/auth.go","lineNumber":197,"sourceCode":"\t\t}\n\n\t\tcfg, err := auth.InstancePrincipalConfigurationForRegionWithCustomClient(common.StringToRegion(p.region), instancePrincipalAuthClientModifier)\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t\tlogger.Debug(\" Configuration provided by: %s\", cfg)\n\n\t\tconfigProviders = append(configProviders, cfg)\n\tcase strings.ToLower(AuthInstancePrincipalWithCertsSetting):\n\t\tlogger.Info(\"Attempting to authenticate using instance principal with certificates\")\n\n\t\tif p.region == \"\" {\n\t\t\treturn nil, fmt.Errorf(\"unable to determine region from Terraform backend configuration while using Instance Principal with certificates\")\n\t\t}\n\n\t\tdefaultCertsDir, err := os.Getwd()\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"can not get working directory for current os platform\")\n\t\t}\n\n\t\tcertsDir := filepath.Clean(getEnvSettingWithDefault(\"test_certificates_location\", defaultCertsDir))\n\t\tleafCertificateBytes, err := getCertificateFileBytes(filepath.Join(certsDir, \"ip_cert.pem\"))\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"can not read leaf certificate from %s\", filepath.Join(certsDir, \"ip_cert.pem\"))\n\t\t}\n\n\t\tleafPrivateKeyBytes, err := getCertificateFileBytes(filepath.Join(certsDir, \"ip_key.pem\"))\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"can not read leaf private key from %s\", filepath.Join(certsDir, \"ip_key.pem\"))\n\t\t}\n\n\t\tleafPassphraseBytes := []byte{}\n\t\tif _, err := os.Stat(certsDir + \"/leaf_passphrase\"); !os.IsNotExist(err) {\n\t\t\tleafPassphraseBytes, err = getCertificateFileBytes(filepath.Join(certsDir + \"leaf_passphrase\"))\n\t\t\tif err != nil {\n\t\t\t\treturn nil, fmt.Errorf(\"can not read leafPassphraseBytes from %s\", filepath.Join(certsDir+\"leaf_passphrase\"))","sourceCodeStart":179,"sourceCodeEnd":215,"githubUrl":"https://github.com/hashicorp/terraform/blob/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/backend/remote-state/oci/auth.go#L179-L215","documentation":"Returned when os.Getwd() fails during InstancePrincipalWithCerts setup. The working directory is used as the default location for test certificate files. This is an OS-level failure: the current directory was deleted, permissions changed, or the process is in an unusual state.","triggerScenarios":"os.Getwd() returns a non-nil error, which can happen if the current working directory has been removed while the process is running, or on certain restricted/containerized environments where the cwd is not accessible.","commonSituations":"Process's working directory was deleted or renamed by another process; running in a container where the cwd mount was removed; restricted sandbox environments; this is rare and indicates an environment issue rather than a config mistake.","solutions":["Run terraform from a directory that exists and is accessible: cd to a valid directory and retry.","Set the 'test_certificates_location' env var to an explicit absolute path so os.Getwd() is only used as a fallback default (though Getwd is called before the fallback applies).","Check container/process working directory configuration if running in CI or containers.","If the issue persists, consider switching to auth=\"InstancePrincipal\" which does not need the working directory."],"exampleFix":"// before\n// running terraform from a directory that was deleted or unmounted\n\n// after\n// ensure cwd exists:\ncd /valid/existing/directory && terraform init","handlingStrategy":"try-catch","validationCode":"// Before running terraform, verify the working directory is accessible:\nfunc validateWorkingDir() error {\n    wd, err := os.Getwd()\n    if err != nil {\n        return fmt.Errorf(\"working directory inaccessible: %w\", err)\n    }\n    if _, err := os.Stat(wd); err != nil {\n        return fmt.Errorf(\"working directory does not exist: %w\", err)\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":"// In your orchestration script:\nif err := validateWorkingDir(); err != nil {\n    log.Printf(\"warning: %v, switching to a known-good directory\", err)\n    if err := os.Chdir(\"/tmp/terraform-work\"); err != nil {\n        log.Fatal(err)\n    }\n}","preventionTips":["Run terraform from a stable, well-known directory.","In CI, explicitly cd to the workspace before running terraform.","Avoid running terraform from ephemeral or temp directories that may be cleaned up."],"tags":["oci","authentication","filesystem","instance-principal","environment","terraform-backend"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}