tbenthompson / cppimport

Import C++ files directly from Python!
MIT License
1.18k stars 67 forks source link

Compiler logs have no correlation to actual lines at which errors occur. #47

Closed 2kai2kai2 closed 4 years ago

2kai2kai2 commented 4 years ago

When I compile and import a .cpp file using cppimport into python, if an error occurs, the lines given for where in the .cpp file the error occurred has no correlation to the actual lines in either the original file or the .rendered.NAME.cpp files. For example, if I get an error message saying there is an error at line 126, I can open both the original and .rendered files and find that line 126 is completely empty.

I am using the MSVC compiler with pybind11, and this happens with any error in any c++ file.

tbenthompson commented 4 years ago

Interesting. I haven't run into this. Could you share an example that produces the issue for you?

2kai2kai2 commented 4 years ago

Here's an example with a pretty simple file

test.py

import cppimport.import_hook

import cppimporttest

cppimporttest.func()

cppimporttest.cpp

// cppimport
/*
<%
import pybind11
import sysconfig
setup_pybind11(cfg)
cfg['include_dirs'] = [pybind11.get_include(), sysconfig.get_path("include")]
%>
*/

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <cstdlib>

namespace py = pybind11; // line 15

void func() {
    py::print("A");
    py::print("B") // Error is here-- no semicolon (line 19)
    py::print("C");
    py::print("D");
}

PYBIND11_MODULE(cppimporttest, m) {
    m.doc() = "test";

    m.def("func", &func);
}

.rendered.cppimporttest.cpp

// cppimport

/*

*/

#include <pybind11/pybind11.h>

#include <pybind11/stl.h>

#include <cstdlib> // line 15 (line 13 in original)

namespace py = pybind11; // line 19 (line 15 in original)

void func() {

    py::print("A");

    py::print("B") // Error is here-- no semicolon (line 27; line 19 in original)

    py::print("C");

    py::print("D");

}

PYBIND11_MODULE(cppimporttest, m) {

    m.doc() = "test";

    m.def("func", &func);

}

Logs

PS C:\Users\2kai2\GitHub\eu4img> & C:/Users/2kai2/AppData/Local/Programs/Python/Python38/python.exe c:/Users/2kai2/GitHub/eu4img/test.py
cl : Command line warning D9002 : ignoring unknown option '-std=c++11'
cl : Command line warning D9002 : ignoring unknown option '-fvisibility=hidden'
.rendered.cppimporttest.cpp
c:\Users\2kai2\GitHub\eu4img\.rendered.cppimporttest.cpp(1): warning C4335: Mac file format detected: please convert the source file to either DOS or UNIX format
c:\Users\2kai2\GitHub\eu4img\.rendered.cppimporttest.cpp(15): error C2143: syntax error: missing ';' before 'pybind11::print'
error: command 'D:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.27.29110\\bin\\HostX86\\x64\\cl.exe' failed with exit status 2

I changed the comments for .rendered.cppimportest.cpp to reflect the accurate lines in that file

tbenthompson commented 4 years ago

I think the issue might be that line breaks are being interpreted differently somehow between your editor and the compiler.

Looking at the .rendered.cppimporttest.cpp file that you copied, it looks like there's a blank line between every line in the original. I have never seen that blank line added before. If I count lines and skip the unexpected blank lines, then the error is occuring on line 15 of .rendered.cppimporttest.cpp which matches with what the compiler is saying. What editor are you opening that file with? Could you try opening that file in a different editor?

2kai2kai2 commented 4 years ago

I'm using VS code, but it does the same thing with notepad++ and windows notepad. I think it could be related to the warning message I get

c:\Users\2kai2\GitHub\eu4img\.rendered.cppimporttest.cpp(1): warning C4335: Mac file format detected: please convert the source file to either DOS or UNIX format

tbenthompson commented 4 years ago

Hmm. So, the issue relates to line endings. Fun reading if you're not familiar: https://blog.codinghorror.com/the-great-newline-schism/

There are two puzzles here then: 1) Why is the Mako templating step producing output with Mac line endings? 2) Is that related to the extra lines showing up in VS code, notepad++, windows notepad. My impression is that Mac line endings are super uncommon and that basically everything either uses windows or unix line endings these days.

Can you try converting to a file with unix line endings and see if that fixes the line number mismatch? If so, then the remaining issue here will be #1. Are you working on a mac?

2kai2kai2 commented 4 years ago

I'm working on Windows, but I think I've solved the problem by forcing VS code to use LF endings instead of CRLF endings for the original .cpp files, so it seems that CRLF line endings result in double newlines. Thanks for your help.