Add support for importing scripts relative the current home directory.
rdar://68310384
Differential D89295
[lldb] Add $HOME to Python's sys.path JDevlieghere on Oct 12 2020, 9:45 PM. Authored by
Details
Add support for importing scripts relative the current home directory. rdar://68310384
Diff Detail Event TimelineComment Actions Could this have undesirable side effects? I wouldn't expect command script import to be searching my home dir. Second question: is there value in requiring the explicit use of ~, for ex: command script import ~/path. Comment Actions What exactly is the use case for this? Having a place where the user can place lldb python scripts and have them be automatically accessible seems like a nice feature, but I am not convinced that $HOME is the right place for it. I definitely wouldn't want lldb scripts to litter my home directory, and if I started putting them in a subfolder, their import paths would get weird (particularly if they want to import one another). I think it'd be better if this was some subdirectory of $HOME. Since we already have $HOME/.lldb, maybe we could put this there (or some subdirectory thereof)? That actually seems to work (if you add .py to the end). Comment Actions Xcode running a project-specific lldbinit file but users can't specify a command script import relative/path/script.py as the CWD for the lldb-rpc-server is /. This makes it impossible to check in custom LLDB Python scripts to an Xcode project that are loaded for all developers (as one can only specify the full path to the Python script). I'm not sure if this patch is actually solving the problem. Now all devs need to have the project in the same path relative to their home directory. I think we could solve this by having Xcode set the CWD to the project's source dir or we add the directories of .lldbinit files to Python's sys.path. Comment Actions Interesting. I think that implementing a project-specific script repository would be indeed best done in xcode, since that's the thing which knows what a "project" is. I'm not sure if this needs any special kind of support from lldb (it sounds like xcode could just modify the import path itself), but if we wanted to add some explicit form of lldb support, I think it should take form of a setting, whose value is automatically added to the import path. Comment Actions Yes that was my (poorly explained) point, I use this form regularly. Is it not better to require an explicit ~ instead of allowing for $HOME as an implicit location. Comment Actions For lldbinit files, and any file that gets command source'd, I think it would be useful if they could perform command script import some/path/to/command.py, where some is resolved relative to the dirname of the lldb file. For example, given an lldbinit file at my/project/scripts/project.lldb, it could load a python at my/project/scripts/commands/my.py by running command script import commands/my.py. Comment Actions +1, I think that would be useful. Not sure about the right way to implement this though (and whether we should make this implicit a relative path vs for example some kind of placeholder 'variable' or something like that). Comment Actions
I'm not sure either, but if we do it could probably be as a ./ as prefix? Comment Actions I misunderstood what was being asked for. In LLDB there are two places we look for an .lldbinit file, in the home directory and in the current working directory. We already add . to the system path so I don't think it's that unreasonable to do the same for $HOME to be able to use imports relative to the .lldbinit file in both cases. In Xcode there's an option to specify a custom "LLDB Init File" which is really nothing more than a HandleCommandsFromFile. So what they're asking is to be able to use any relative import when sourcing a file. Comment Actions PS: when I created this patch I used $HOME in the title, I didn't expect that to get expanded?! Comment Actions
In theory I agree, but in practice I think it's unnecessary because ~ can be used (and possibly should be explicit), and because $HOME is a weird place to put lldb python scripts, as @labath stated. Should we create some kind of $PATH variable for lldb to use for loading scripts? People could set this in their ~/.lldbinit? This still wouldn't resolve the original bug report, but it may be a nice general addition. Comment Actions Other than as a way to the test this change I never advocated putting Python scripts in your home directory. And playing the Devil's advocate: I think the same argument could be made for using ./ as for ~. If it were up to me I still think there's some value in making the two consistent, but I like I said I misunderstood the request so I don't feel strongly enough to fight for it.
They could already do what AddToSysPath does by hand if they really wanted to. I think this is more about improving the UX. script sys.path.append("$SRCROOT") Comment Actions That would be kind of useful, but the thing which worries me about that is the sys.path packing it will cause. I don't think that sys.path modification should be taken lightly, as every new entry there creates an opportunity for creating ambiguous imports (being able to import one file through several paths). And a lot of "interesting" things can happen when a python module gets imported through multiple paths. I think it's bad enough that every "command script import" adds a new sys.path entry. I wouldn't want "command source" to do the same... Comment Actions The way I started implementing it is that the CommandInterpreter keeps a stack of FileSpecs and when you do command script import it asks the current interpreter for the current one and passes it along. So rather than always adding a new sys.path entry when sourcing a file, we now insert two sys.path entries when doing a command script import when sourcing and still one otherwise. Does that sound like a reasonable compromise? |