{"record":{"id":"a61fe08ee8fc64d9","repo":"grafana/k6","slug":"creating-temporary-directory-for-downloads-w","errorCode":null,"errorMessage":"creating temporary directory for downloads: %w","messagePattern":"creating temporary directory for downloads: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/browser_context.go","lineNumber":102,"sourceCode":"\t// DownloadsPath is the path where downloads will be stored.\n\tDownloadsPath string\n}\n\n// artifactsDirectory is the prefix for the temporary directory created for downloads.\nconst artifactsDirectory = \"k6browser-artifacts-\"\n\n// setDownloadsPath sets the downloads path.\n// If the provided path is empty, a temporary directory with\n// an artifactsDirectory prefix will be created.\nfunc (b *BrowserContext) setDownloadsPath(path string) error {\n\tpath = strings.TrimSpace(path)\n\tif path != \"\" {\n\t\tb.DownloadsPath = path\n\t\treturn nil\n\t}\n\tdir, err := os.MkdirTemp(os.TempDir(), artifactsDirectory+\"*\") //nolint:forbidigo\n\tif err != nil {\n\t\treturn fmt.Errorf(\"creating temporary directory for downloads: %w\", err)\n\t}\n\tb.DownloadsPath = dir\n\n\treturn nil\n}\n\n// cleanup cleans up the resources associated with the browser context.\nfunc (b *BrowserContext) cleanup() error {\n\tif err := os.RemoveAll(b.DownloadsPath); err != nil { //nolint:forbidigo\n\t\treturn fmt.Errorf(\"removing downloads path: %w\", err)\n\t}\n\tb.DownloadsPath = \"\"\n\n\treturn nil\n}\n\n// NewBrowserContext creates a new browser context.\nfunc NewBrowserContext(","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/browser_context.go#L84-L120","documentation":"When a browser context is created without an explicit downloadsPath, setDownloadsPath (browser_context.go:102) calls os.MkdirTemp(TMPDIR, 'artifacts-*') to create a scratch directory for downloaded files. If the OS refuses — unwritable or missing TMPDIR, ENOSPC, EACCES — context creation fails with 'creating temporary directory for downloads: %w' carrying the syscall error.","triggerScenarios":"TMPDIR unset, missing or unwritable; read-only root filesystem in a hardened container; disk full; CI runner with a tiny /tmp; security policy blocking writes under /tmp.","commonSituations":"Minimal distroless/scratch containers; Kubernetes pods with readOnlyRootFilesystem and no writable emptyDir for /tmp; disk quotas hit in CI.","solutions":["Pass an explicit writable path: browser.newContext({ downloadsPath: '/writable/dir' }) on a volume you control","Set TMPDIR to a writable directory for the k6 process","Free disk space or raise emptyDir/ephemeral-storage limits if ENOSPC","With readOnlyRootFilesystems, mount a writable tmpfs/emptyDir at /tmp"],"exampleFix":"// before\nconst ctx = browser.newContext() // auto temp dir in unwritable /tmp\n\n// after\nconst ctx = browser.newContext({ downloadsPath: '/mnt/vol/downloads' })","handlingStrategy":"validation","validationCode":"import { check } from 'k6'\n// ensure a writable downloads dir before creating the context\nconst ctx = browser.newContext({ downloadsPath: '/writable/k6-downloads' })","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always pass an explicit downloadsPath on a writable volume","Set TMPDIR to a writable directory in hardened containers","With readOnlyRootFilesystem, mount a writable emptyDir at /tmp"],"tags":["filesystem","temp-dir","downloads","container","environment"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}