-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
In-place edit (-i
) issues (was: assertion failure in main.c line 518 & coredump)
#704
Comments
Thanks for the report! |
Fixed. This time with a test. |
Don't think it's quite right - although I don't get the assert, the first file ends up containing the merged content of both the first and second files, and the first file contains all the syntax-colouring escape codes even though the output is a file. |
The coloring is clearly a bug (fix coming up), but What would the alternative be? To in-place edit every file? I suppose. What do others thing? |
-i
) issues (was: assertion failure in main.c line 518 & coredump)
@nicowilliams asked:
Yes. The inspiration (if not model) for jq's -i option seems to be sed's -i option, and sed's behavior is perfectly reasonable in this respect (as in others). To avoid confusion, I think it would be reasonable for jq to take the view (so to speak) that the -i option is a switch (!) that is intended to change its behavior to be more like (maybe even much more like) sed's. (Yes, I often use -i.bak) |
In-place editing of each file is a significant rototill, particularly after the refactoring I did of main.c. I'll have to make |
@nicowilliams wrote:
Maybe the appropriate thing to do is to take -i out of 1.5. As for color -- as I suggested, I think you'll feel greatly liberated if you take the view that -i is a SWITCH! |
The documentation for -i says "Edit the (first) file in place" so I guess what it's doing now is (partly) as advertised, what I didn't expect to happen is for all the following files to be inserted into the first one as that doesn't really seem to be an in-place edit. I think there are perhaps 3 choices:
I think the first is probably the best, but I understand it might be a fair amount of work. |
I'll make it do the right thing. For color I'll have it recompute |
Thanks. As an aside, I hadn't yet figured out that there was a libjq, interesting - I'll go investigate. |
@alanbur Yeah, take a look at libjq if it's of any use to you. There are two APIs in it: the jv API (everything to do with parsing and formatting JSON texts, and the internal representation of JSON values) and the jq API (everything to do with evaluating jq programs). @stedolan's jv API is the best C JSON API I have seen yet. In particular it's built around COW, and this makes it very easy to write C code that uses this API. The code styling is @stedolan's, and it's not at all like the Solaris cstyle that you and I are accustomed to. EDIT: I should add that I find @stedolan's cstyle very comfortable. |
Alright, the new plan is as follows:
It should be possible to do something like:
This is nice in part because it means there's no need to add more options to say "remaining arguments are strings". OTOH, this means that positional order of options becomes [more] important. I can live with that. |
@nicowilliams wrote:
Why not adopt a sed (or at least sed-like) approach, i.e. with a preference (or requirement) for a suffix? (Since sed does not (as a matter of policy) require true in-place editing (same inode), making the suffix a requirement would not be such a bad thing, would it?) |
I'll think about the sed |
@nicowilliams wrote:
It's a minor point, but I think there is a useful sense in which More importantly, it seems to me that there's some kind of lesson here, regarding seemingly small changes that turn out to be significant distractions. It still seems to me that pulling "-i" from 1.5 might be the wise thing to do. |
The problem with "true" in-place editing is the lack of atomicity. A SQLite3 can manage fine. An interactive editor can also do in-place edits (because it can have a backup file and offer a recovery mode). But if a file is being watched for, the only atomic operations that work are rename and unlink. (Yes, that's POSIX, but Win32 nowadays allows a file opener to request POSIX semantics as to renames and unlinks, FWIW.) As jq is not interactive, I think the right thing to do is to rename into place -- of that much I am convinced. |
@nicowilliams thanks for the libjq info. I speak Scala, so 2-space indents and functional-style programming are perfectly comfortable :-) |
The new plan is to process files as file arguments are encountered instead of accumulating them in a list to process after all option processing is complete. This will allow slurping some files and not others, for example, if we make Where does that leave I should extend
instead of having to:
|
The other thing is that this really begs for a builtin to do this from within a jq program. I know... |
In-place editing should be implemented with builtins for file I/O.
b82c231 "Remove -i option (jqlang#704)" removed its last usage in 2015. Spotted while looking for code could potentially write/create/modify files.
This behaviour seems a little severe:
The text was updated successfully, but these errors were encountered: