Repository navigation
Description
Update:
For anyone reading this, jump straight to this comment, which has all the information
#6384 (comment)
CC @kevinushey since we talked about this
System details
RStudio Edition : Desktop
RStudio Version : 1.3.869
OS Version : Mac
R Version : 3.6.0
Setup
I have an R package with C code. I plan to model the src/ directory after rlang. The way this works is to have:
- Possibly multiple folders of code, such as
src/lib/ - A corresponding top level file of the same name as the folder, like
src/lib.c- This file
#includes all of the .c files in thesrc/lib/folder
- This file
- A Makevars to compile this
The Makevars controls the compilation like this:
lib-files = lib/code2.c
all: $(SHLIB)
$(SHLIB): lib.o
lib.c: $(lib-files)
touch lib.c
.PHONY: all
To the best of my knowledge, this works by telling the $(SHLIB) to wait until lib.o is ready, and for that to happen we compile lib.c. We compile lib.c if any of the files in lib/ have changed and then touch it to ensure we perform this check again next time we load_all().
Problem
The problem is that when I set it up in this way, Clang's autocomplete engine fails to autocomplete anything from <R.h> or <Rinternals.h> anymore. I've set up a dummy package to demonstrate this:
Try going into code.c or lib/code2.c and deleting ATTRIB() and trying to get it to autocomplete. It should fail.
Turning on .rs.setClangDiagnostics(2) gives me:
CLANG INDEXING: /Users/davis/Desktop/r/playground/packages/testnestedclang/src/code.c
CLANG REMOVE INDEX: /Users/davis/Desktop/r/playground/packages/testnestedclang/src/code.c
COMPILATION ARGUMENTS:
[
"-isystem",
"/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include",
"-I/Library/Developer/CommandLineTools/usr/lib/clang/10.0.1/include",
"-I/usr/local/include",
"-I/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1",
"-I/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/11.0.0/include",
"-I/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include",
"-I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include",
"-v"
]
(Creating new index)
Apple LLVM version 10.0.1 (clang-1001.0.46.4)
Target: x86_64-apple-darwin18.6.0
Thread model: posix
InstalledDir:
ignoring nonexistent directory "/usr/include"
ignoring duplicate directory "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include"
as it is a non-system directory that duplicates a system directory
ignoring duplicate directory "/usr/local/include"
as it is a non-system directory that duplicates a system directory
ignoring duplicate directory "/Library/Developer/CommandLineTools/usr/lib/clang/10.0.1/include"
as it is a non-system directory that duplicates a system directory
#include "..." search starts here:
#include <...> search starts here:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/11.0.0/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include
/usr/local/include
/Library/Developer/CommandLineTools/usr/lib/clang/10.0.1/include
/Library/Developer/CommandLineTools/usr/include
/System/Library/Frameworks (framework directory)
/Library/Frameworks (framework directory)
End of search list.
/Users/davis/Desktop/r/playground/packages/testnestedclang/src/code.c:2:10: fatal error: 'R.h' file not found
TOTAL MEMORY: 379 kb (code.c)
PERFORMANCE 1.9 ms (code.c)
/Users/davis/Desktop/r/playground/packages/testnestedclang/src/code.c:2:10: fatal error: 'R.h' file not found
The main thing I notice is the fatal error: 'R.h' file not found. This seems to be a side effect of this not showing up in the #include search /Library/Frameworks/R.framework/Resources/include, which generally does show up in other R packages using C code
Activity
DavisVaughan commentedon Mar 25, 2020
Okay I can offer much more insight here.
I have a second simpler package here
https://github.com/DavisVaughan/testnestedclang2
If you go into
src/code.con the master branch, code completion works.If you change to the
not-workingbranch, and then go into thesrc/code.cfile, code completion is not working.The only difference between the two branches is that the
not-workingone has a second top level file (src/code3.c).When autocompletion works, the compilation args from
.rs.setClangDiagnostics(2)look like:when it fails:
So you can see that the chunk of includes starting with
"-I/Library/Frameworks/R.framework/Resources/include"are missing, which is why we getfatal error: 'R.h' file not foundfrom above.These includes are normally retrieved from RStudio running a
R CMD SHLIB --dry-runcall in a temp directory where a dummy C file is created named after one of the package's C files. The package's Makevars is also copied here so any includes inPKG_CFLAGSare picked up.That all happens here
rstudio/src/cpp/session/modules/clang/RCompilationDatabase.cpp
Lines 339 to 408 in 22c112c
My guess (which @kevinushey also agrees with) is that this is failing for some reason. His exact words: "I suspect the way we form this temporary directory + file is breaking R CMD SHLIB here."
DavisVaughan commentedon Mar 25, 2020
Kevin also mentioned: "I don't recall exactly why we do this dance, though. As opposed to just calling R CMD SHLIB directly in the src directory"
I think you have to create the temp directory because if you run
R CMD SHLIB --dry-runon a src directory that has already been compiled and is up to date, then the underlying make call won't spit out any information on the includes that would be used (which is what RStudio captures and parses). By creating a temp directory with a dummy C file, you guarantee that compilation is required.kevinushey commentedon Mar 25, 2020
Perhaps the solution here is to preserve the directory structure, but only copy source files (leave out any compiled artefacts)
DavisVaughan commentedon Mar 25, 2020
That might be useful as a more general solution because I'm also thinking of structuring a package with folders, and no top level C files
so just looking at the children files of
src/wouldn't be good enough to find one that can be used in the SHLIB command. You'd either have to look recursively or preserve the directory structure as you sayDavisVaughan commentedon Mar 27, 2020
Is it possible to get this include information in a different way? For example, you could look at the package level
Makevarsfile and look for-Ito get those.For the R level includes, they seem to be defined in the Makeconf file? The Matrix package finds the Makeconf file with this
$(R_HOME)/etc${R_ARCH}/Makeconfhttps://github.com/cran/Matrix/blob/13baa504e0e1ede81c41388d01ae28662acf62d8/src/Makevars#L7
And then it seems like
.cfiles useALL_CPPFLAGSandALL_CFLAGS, which I found out by looking in my MakeconfFull makeconf:
Details
DavisVaughan commentedon Mar 27, 2020
Or maybe you could look in the package level Makevars for
-Iand parse that, and then you won't need to care about the package Makevars when running SHLIB.So you could run
R CMD SHLIBin a temp directory with a single dummy C file and not copy over the package Makevars. Then you won't need to name it anything in particularDavisVaughan commentedon Aug 10, 2020
I am somewhat confident I know what is happening here. The TL;DR is that RStudio is only copying 1 file of the src/ directory over to a temp directory to run R CMD SHLIB in, but it is also copying the entire Makevars.
If the Makevars uses a custom OBJECT ordering, and the first OBJECT of that Makevars corresponds with the file name of the 1 copied file, then R CMD SHLIB will work and will spit out the compilation information of that 1 file, and will then error on the next OBJECT (because that OBJECT won't have an equally named file). This is enough information for RStudio to parse to get the compilation args.
However, if the 1 file copied over is not the same as the first OBJECT, then R CMD SHLIB will error immediately, and no compilation info is displayed, so RStudio has nothing to parse.
I think the solution is as you suggested earlier. Preserve the directory structure but only copy source files (without any artifacts so R CMD SHLIB will do something).
This actually has nothing to do with having subdirectories in src/. It instead has to do with having a custom OBJECT ordering in a Makevars.
It is worth noting that the file that gets copied over into the temp directory has an undefined ordering, as it is the first
.cfile found by iterating over the src/ directory with a boostdirectory_iterator, and this page states: "The order of directory entries obtained by dereferencing successive increments of a directory_iterator is unspecified." This is just useful to keep in mind that it is not necessarily the first file in lexicographic order.As proof of all of this, I have constructed a third example package. It is much simpler than the others because it doesn't contain any subdirectories in src/
https://github.com/DavisVaughan/testnestedclang3
The master branch holds a non-working example. There are two files,
code.candcode3.c, and a custom Makevars which species the OBJECTS to process. I suspect thatdirectory_iteratorfindscode3.cfirst, so that is the file that gets copied into the temp directory. R CMD SHLIB chokes on this, because the Makevars that also gets copied over specifies thatcode.oshould come first.I have confirmed my theories in a few ways, described below:
Proof 1 - Swap OBJECT ordering
Simply swapping the OBJECT ordering in the Makevars so that
code3.ocomes first makes auto-complete work again. Like:This would be proof that
directory_iteratoris findingcode3.cfirst and that is the file that gets copied over.Proof 2 - Rename
code3.cFor some reason, if I rename
code3.ctocode2.c(and rename the corresponding OBJECT in the Makevars), then it starts working again. I imagine that somehow thedirectory_iteratornow findscode.cfirst, and that aligns with the first OBJECT so all is well. Again, the ordering thatdirectory_iteratoruses when iterating is undefined, so this seems like a reasonable assumption.I did this in this working branch, the PR shows the changes
DavisVaughan/testnestedclang3#1
Proof 3 - R CMD SHLIB output
This is really just a test of R CMD SHLIB directly so I can show that the output of it does depend on the 1 file that gets copied over by RStudio. I'm going to mock the two possible choices of what RStudio might be doing (either copying over
code.corcode3.c).In the master branch of testnestedclang3, do the following:
devtools::clean_dll()Delete
code3.c. This mocks copying overcode.candMakevarsto a tempdir.In terminal, change to the src/ directory of this package and run:
R CMD SHLIB --dry-run code.c.Above I see the
clangline containing the compilation args that RStudio parses, which makes sense.Now, we mock the other way, by deleting
code.c.devtools::clean_dll()Delete
code.c. This mocks copying overcode3.candMakevarsto a tempdir.In terminal, change to the src/ directory of this package and run:
R CMD SHLIB --dry-run code3.c.Above I do not see the
clangline, so RStudio has nothing to parse! This happens because the Makevars specifiescode.oas the first object, butcode3.cis the file that is copied over. I believe this is what is currently happening.[-]Clang autocompletion with Makevars and nested C folders [/-][+]Clang autocompletion with Makevars relies on random ordering[/+]kevinushey commentedon Jan 3, 2022
@jennybc is now bumping into this with
readxl, which similarly uses OBJECTS:https://github.com/tidyverse/readxl/blob/main/src/Makevars
The relevant invocation of R CMD SHLIB lives here:
rstudio/src/cpp/session/modules/clang/RCompilationDatabase.cpp
Lines 442 to 465 in 9bbfe5b
The main question is, I think -- why don't we just preserve the file names as-is? Do we really need to make these names unique, if we're already doing the work to work in a custom temporary directory?
(For that matter, why not just call
R CMD SHLIB --dry-rundirectly in thesrc/directory? Presumedly we do this dance for a reason; e.g. to avoid R writing some unexpected outputs into thesrc/directory, but is that really necessary?)This loop also looks kind of bogus to me...
rstudio/src/cpp/session/modules/clang/RCompilationDatabase.cpp
Lines 452 to 461 in 9bbfe5b
DavisVaughan commentedon Jan 3, 2022
I think that was because of this:
#6384 (comment)
6 remaining items
romainfrancois commentedon Aug 30, 2022
It seems that it does the trick. e.g. here with
readxlon build 153:Enregistrement.de.l.ecran.2022-08-30.a.10.03.35.mov
as opposed to build 152:
Enregistrement.de.l.ecran.2022-08-30.a.10.09.14.mov
romainfrancois commentedon Aug 30, 2022
one pitfall though is that the compilation setting might be restored, so with build 153 you have to force it to rebuild, e.g. by modifying the
Makevarsfile.Trying to fix this in this follow up: #11865
DavisVaughan commentedon Aug 30, 2022
@kevinushey yea I'm definitely up for trying it out on a few packages once @romainfrancois feels like it is in a decent place
romainfrancois commentedon Aug 30, 2022
I believe it's good to go, but should be easier to test once #11865 is merged
romainfrancois commentedon Aug 31, 2022
rlangworks for me with build 157.DavisVaughan commentedon Aug 31, 2022
rlang and readxl do seem to be working now!
It seems like {ps} isn't working with autocomplete still. I searched for packages that use
OBJECTS. This is my compilation output with the latest daily after touchingMakevars(so you can see the make cmd). It looks like themakepart is failing because it saysmake would usebut nothing after that like it normally doesFYI, when working on {ps} you might have to
load_all()once, which forces it to auto-generate aconfig.hfileHere are my details:
Details
Another one to try would be processx, which also uses OBJECTS (update: looks like this doesn't work either)
kevinushey commentedon Aug 31, 2022
It's working for me 😞
EDIT: Ah, it stops working after I
devtools::load_all(). So somehow that's implicated here... it seems like it creates / mutates a Makevars file and that causes things to stop working.kevinushey commentedon Aug 31, 2022
The extra dependencies in the Makevars file are confusing RStudio here.
kevinushey commentedon Aug 31, 2022
processxis likely to have similar issues since thealltarget depends on building other tools.kevinushey commentedon Aug 31, 2022
I don't know what the best solution here is.
R CMD SHLIB --dry-runwork in thesrc/directory even if the.oand.sotargets already exist?R CMD SHLIB?DavisVaughan commentedon Aug 31, 2022
I feel like 3 would be a nice option if possible, since that keeps everything "in place"
The
R CMD SHLIB --dry-runstuff only runs when you recompile the database right? Which is an extremely uncommon thing to do? i.e. it seems to only happen for me if I touchMakevars.What if
.oand.sotargets were just cleaned out by RStudio before it had to run this, just soR CMD SHLIB --dry-runcould be run?If it only happened once in a blue moon then I wouldn't be mad about having to recompile after it
kevinushey commentedon Aug 31, 2022
My main worry would be if we did this while the user was already trying to build / compile things in the package. I'm not sure if there's a clean way for us to avoid that.
Maybe (at least on Linux / macOS) we could symlink all of the non-object files into the compilation directory? That should be relatively cheap...
mikebessuille commentedon Sep 13, 2022
@DavisVaughan this fix is now in the latest daily; could you please have a look and see if it resolves the problem?
DavisVaughan commentedon Sep 13, 2022
I tried with vctrs, slider, ps, readxl, and rlang and all seem to be working as expected!