Skip to content

Python Injection: "Failed to inject '...': Invalid \escape..." #864

@oudajosefu

Description

@oudajosefu

Bug Description

When executing a Python cell that references a SQL variable injection (using @var syntax) in a DBCode notebook, the operation fails with two sequential errors:

  1. First Error (DBCode Output): Failed to inject 'test': Invalid \escape: line 1 column 329 (char 328)
  2. Second Error (Python Execution): Error: name 'test' is not defined

The issue occurs when DBCode attempts to inject SQL query results from a declared variable into the Python execution environment. The initial JSON parsing error prevents the variable from being injected, which subsequently causes the Python NameError when the code tries to reference the undefined variable.

Repro steps

  1. Open a DBCode notebook file (.dbcnb)
  2. Create a SQL cell with a variable declaration: -- @var test followed by a SQL query containing special characters (e.g., select top 20 * from Table where Column like '%test%';)
  3. Execute the SQL cell - it completes successfully and returns results
  4. Create a Python cell that references the injected variable (e.g., import pandas as pd and print(test.head()))
  5. Attempt to execute the Python cell
  6. Observe error in DBCode Output panel: Failed to inject 'test': Invalid \escape: line 1 column 329 (char 328)
  7. Python cell execution fails with: Error: name 'test' is not defined

Expected Behavior

  • The SQL query results stored in the test variable should be successfully injected into the Python cell's execution context as a pandas DataFrame
  • The Python code should execute without errors and be able to access the data (e.g., calling .head() method to display the first few rows)
  • No JSON escape sequence errors should occur during the injection process

Actual Behavior

  • DBCode fails to inject the SQL variable due to a JSON serialization error with escape sequences
  • The DBCode Output panel shows: Failed to inject 'test': Invalid \escape: line 1 column 329 (char 328)
  • Because the injection fails, the variable is never created in the Python environment
  • When the Python cell attempts to execute, it fails with: Error: name 'test' is not defined
  • This makes the Python injection feature completely non-functional for affected queries

Environment

  • DBCode version: 1.19.9
  • VS Code (or fork) version: 1.106.3 (user setup)
  • OS: Windows
  • Database: Microsoft SQL Server (MSSQL)
  • Connection: Direct

Error Messages (if applicable)

DBCode Output Panel:

2025-12-04 17:56:37.940 [error] Failed to inject 'test': Invalid \escape: line 1 column 329 (char 328)

Python Cell Execution:

Error: name 'test' is not defined

Logs

2025-12-04 17:56:37.940 [error] Failed to inject 'test': Invalid \escape: line 1 column 329 (char 328)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions