At that time, I was working on a feature that, as part of its functionality, made a parameterized SQL query to the database. In EF Core, there's a specific function for this called FromSql - you write the query in SQL, and the framework automatically creates objects from the results. Everything worked perfectly when running locally and on the test environment (k8s), where the artifacts from the build server (docker images) were deployed.
Soon, the QA team contacted me, asking why the feature wasn't working in their personal environments (local k8s + docker images, built on their local machines). This was quite strange, as the same scripts were used to build both the local images and the images on the build server.
I had no choice but to start debugging directly in the running Docker container and see what was going wrong. Eventually, the picture became clear:
1. The QA team was working on Windows machines.
2. In Git, automatic conversion of line endings to Windows-style was enabled when checking out.
3. The Docker images were built from local source code, which had Windows-style line endings.
4. The SQL query in the source code was written as a multi-line verbatim string literal. That is, the string was compiled with Windows-style line endings.
5. The personal environments were running in containers that operated on Unix.
6. The parser (EF Core/Npgsql), which validated the passed SQL code, expected to see either arguments or a correct platform-specific line break (Environment.NewLine) after the
SELECT statement, if each argument was on a separate line. I couldn't find the exact code, but it's likely that it has been rewritten several times since then.7. In our case, the string looked like this:
SELECT
ARG1,
ARG2,
...
8. Since the source code was compiled with Windows-style line endings, the parser didn't see the correct line break and threw an exception, indicating that the query was invalid.
9. The problem was solved by removing the line break after
SELECT:SELECT ARG1,
ARG2,
...
The moral of the story:
1. Source code with Windows-style line endings, compiled for Unix, may not work as expected.
2. External dependencies can sometimes cause unexpected problems.
3. Debugging in a Docker container can sometimes be very helpful.
4. Sometimes, a simple line break for code formatting can lead to a bug.
Have you encountered anything similar?
#work #programming #bugs
