{"record":{"id":"0e306909b496e671","repo":"astral-sh/ruff","slug":"failed-to-collapse-with-err","errorCode":null,"errorMessage":"Failed to collapse `with`: {err}","messagePattern":"Failed to collapse `with`: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"info","filePath":"crates/ruff_linter/src/rules/flake8_simplify/rules/ast_with.rs","lineNumber":206,"sourceCode":"                    checker.stylist(),\n                    with_stmt,\n                ) {\n                    Ok(edit) => {\n                        if edit.content().is_none_or(|content| {\n                            fits(\n                                content,\n                                with_stmt.into(),\n                                checker.locator(),\n                                checker.settings().pycodestyle.max_line_length,\n                                checker.settings().tab_size,\n                            )\n                        }) {\n                            Ok(Some(Fix::safe_edit(edit)))\n                        } else {\n                            Ok(None)\n                        }\n                    }\n                    Err(err) => bail!(\"Failed to collapse `with`: {err}\"),\n                }\n            });\n        }\n    }\n}\n","sourceCodeStart":188,"sourceCodeEnd":212,"githubUrl":"https://github.com/astral-sh/ruff/blob/26f38c119cac42e4d320ba08f09224fdec74af2c/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_with.rs#L188-L212","documentation":"In rule SIM117 (multiple `with` statements should be collapsed into one), the fix pipelines the statements through a libcst-based collapse helper; when that helper returns an `Err`, the diagnostic check wraps it in this bail message that includes the underlying error. The lint finding still fires, but no autofix is produced.","triggerScenarios":"`ruff check --fix` on nested/sequential `with` statements whose collapse fails internally — for example `with open(a) as f, open(b) as g:` bodies the libcst transformer cannot re-indent, or a `with` at module level where indentation cannot be inferred.","commonSituations":"Multiline `with` statements with comments or parenthesized context managers, files with mixed indentation (tabs/spaces), and generated code where the statement range doesn't round-trip through libcst.","solutions":["Manually merge the consecutive `with` statements into a single `with A as a, B as b:` block and rerun ruff","Inspect the inner `{err}` message in the output to see which libcst step failed and fix the syntax it objects to (comments, blank lines, indentation)","Skip the fix with `# noqa: SIM117` if keeping separate `with` blocks is intentional"],"exampleFix":"// before\nwith open(a) as f:\n    with open(b) as g:\n        ...\n// after\nwith open(a) as f, open(b) as g:\n    ...","handlingStrategy":"try-catch","validationCode":"# pre-check: consecutive with statements without interleaving comments are usually fixable\npython - <<'EOF'\nimport ast, sys\ntree = ast.parse(open(sys.argv[1]).read())\nfor n in ast.walk(tree):\n    if isinstance(n, ast.With):\n        if isinstance(getattr(n, 'body', [None])[0], ast.With):\n            print('SIM117 candidate at line', n.lineno)\nEOF","typeGuard":null,"tryCatchPattern":"# apply the autofix, and on failure do it by hand:\nruff check --select SIM117 --fix path/ || echo 'manual collapse required; see ruff output for inner error'","preventionTips":["Avoid comments between collapsible `with` statements","Keep one context manager per `with` header line style consistent","Run `ruff check --diff` first to see which SIM117 findings carry fixes"],"tags":["ruff","flake8-simplify","sim117","autofix"],"backgroundTag":"linter-autofix-skipped","analyzedSha":"26f38c119cac42e4d320ba08f09224fdec74af2c","analyzedAt":"2026-09-05T10:32:37.492Z","contentChangedAt":"2026-09-05T10:32:37.492Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}