vyperlang / vvm

Vyper version manager
MIT License
25 stars 9 forks source link

feat: compile fns pass kwargs to vyper wrapper #15

Closed z80dev closed 2 weeks ago

z80dev commented 11 months ago

What I did

The non-json compile functions exposed by VVM do not expose an interface for passing additional compiler flags to vyper when invoked.

This functinality, along with conversion of flags like "foo_bar" into "--foo-bar", is already implemented in vvm's wrapper module.

this PR simply passes along any uncaptured kwargs passed to compile or compile_files to enable specific use-cases.

because flags may be inconsistent across vyper versions, validation is left out of scope here. we're not offering options to the user, just letting them pass along ones they determine will be correct.

This currently does not support every case. There are some flags which we can pass that result in invalid json being output, which vvm will fail to parse. However this unblocks cases that do work (namely, building just the AST, for vyper-lsp performance)

How I did it

added kwargs to compile and compile_files, and passed that arg to the call to vyper.vyper_wrapper(..)

if the f kwarg is present, we handle it specially, since we were hardcoding that to combined_json

There are some values for f that result in invalid JSON output from the compiler. This is currently as designed, but maybe should change. Perhaps if you want json you need to use vyper-json, but currently some output formats from plain vyper are valid JSON and others are not.

I added error handling for whenever a user specifies an output format that is not valid JSON. This will catch errors from any flags that result in non-JSON output.

Whether we should only use vyper-json when we need JSON output (therefore all of VVM should switch to only using vyper-json is another question to be discussed, but this enables AST construction with the fewest changes while we determine the best way forward.

How to verify it

pip install git+https://github.com/z80dev/vvm@pass-flags

import vvm

src = """
x: uint256

@external
def foo() -> uint256:
    return self.x
"""

vvm.compile_source(src, f='ast')
~/Developer/vvm pass-flags*
❯ python poc.py
{'<stdin>': {'ast': {'node_id': 0, 'body': [{'node_id': 1, 'is_public': False, 'is_immutable': False, 'src': '1:10:0', 'target': {'id': 'x', 'node_id': 2, 'ast_type': 'Name', 'col_offset': 0, 'end_col_offset': 1, 'end_lineno': 2, 'src': '1:1:0', 'lineno': 2}, 'ast_type': 'VariableDecl', 'col_offset': 0, 'end_col_offset': 10, 'value': None, 'is_transient': False, 'annotation': {'id': 'uint256', 'node_id': 4, 'ast_type': 'Name', 'col_offset': 3, 'end_col_offset': 10, 'end_lineno': 2, 'src': '4:7:0', 'lineno': 2}, 'end_lineno': 2, 'is_constant': False, 'lineno': 2}, {'node_id': 6, 'pos': None, 'body': [{'node_id': 8, 'ast_type': 'Return', 'col_offset': 4, 'value': {'node_id': 9, 'attr': 'x', 'ast_type': 'Attribute', 'col_offset': 11, 'value': {'id': 'self', 'node_id': 10, 'ast_type': 'Name', 'col_offset': 11, 'end_col_offset': 15, 'end_lineno': 5, 'src': '46:4:0', 'lineno': 5}, 'end_col_offset': 17, 'end_lineno': 5, 'src': '46:6:0', 'lineno': 5}, 'end_col_offset': 17, 'end_lineno': 5, 'src': '39:13:0', 'lineno': 5}], 'name': 'foo', 'doc_string': None, 'ast_type': 'FunctionDef', 'returns': {'id': 'uint256', 'node_id': 13, 'ast_type': 'Name', 'col_offset': 13, 'end_col_offset': 20, 'end_lineno': 4, 'src': '26:7:0', 'lineno': 4}, 'decorator_list': [], 'col_offset': 0, 'args': {'node_id': 7, 'defaults': [], 'ast_type': 'arguments', 'col_offset': 0, 'args': [], 'end_col_offset': 3, 'default': None, 'end_lineno': 4, 'src': '13:3:0', 'lineno': 4}, 'end_col_offset': 17, 'end_lineno': 5, 'src': '13:39:0', 'lineno': 4}], 'doc_string': None, 'name': '/var/folders/pw/hxmzlpdx18193c72f7pnf4xm0000gn/T/vyper-vjhn1vtf.vy', 'ast_type': 'Module', 'col_offset': 0, 'end_col_offset': 17, 'end_lineno': 5, 'src': '0:52:0', 'lineno': 1}}}

Checklist

DanielSchiavini commented 2 weeks ago

Sorry but it looks like this is a duplicate of #21

charles-cooper commented 2 weeks ago

closing as superseded by #21